Introduction To Tcl

From OpenSeesWiki
Revision as of 22:50, 1 April 2010 by Fmk (talk | contribs)
Jump to navigation Jump to search




Commands

Tcl is a string-based command language with relatively little syntax. Tcl scripts are made up of commands separated by new lines or semicolon (;). The basic syntax for a Tcl command is:

command arg1 arg2 arg3 ...


command is name of the command
$arg1 $arg2 $arg3 ... are the arguments for the command


The command is either the name of a built-in command or a Tcl procedure defined previously by the user. White space (i.e., space or tab) is used to separate the command name and its arguments, and a newline or semicolon is used to terminate a command. The arguments to a command are just strings. Here is an example:

puts "Hello World"

The puts is the command. In this example it takes one argument. In this form puts will take the single string argument at write it to the terminal. The quotation marks " are used to group the words into a single argument, { } braces could also have been used. Tcl has syntax for grouping, which allows multiple words in one argument, and substitution, which is used with programming variables and nested command calls. The Tcl interpreter does grouping first, then substitutions, and finally it calls the command. It is up to the command to interpret its arguments.

Variables

Another basic command in tcl is the set command. It is used to assign a value to a variable.

set msg "Hello World"

In this example we are setting the variable msg to be equal to the string Hello World'.

Mathematical Expressions

The Tcl interpreter itself does not evaluate math expressions. Instead, the expr command is used to evaluate math expressions. The interpreter treats expr just like any other command, and it leaves the expression parsing up to the expr implementation. The math syntax supported by expr is the same as the C expression syntax. The expr command deals with integer, floating point, and boolean values. Logical operations return either 0 (false) or 1 (true).


A word or warning, integer values are promoted to floating point values as needed, WHICH MEANS THE USER MUST BE WARY OF INTEGER DIVISION.

set a [expr 3 /2.0]
=> 1.5
set b [expr 3/2]
=> 1.0
set c [expr 3.0/2]
=> 1.5