Tcl Syntax

  1. Every script is a sequence of commands separated by newlines or semicolons.
  2. All commands are formed by one or more words separated by spaces or tabs.
    CMD arg arg arg ...
  3. The first word is always the name of the command.
  4. The remaining words are the arguments.
  5. Even ``if'', ``while'', ``for'', ``foreach'' and ``switch'', and the definition of new procedures obey this rule.
  6. If the first character of a command is a ``#'' the text that follows (up to the next newline) is treated as a comment.
Example:
% set  a  10
% puts "This is the value of a: $a "
% while {$a > 0} { puts $a }
% if { $a == 0 } { puts "a=0" } else { puts "a!=0" }

Tcl Special Symbols

Table 1.
Symbol    
$ Dollar sign Used for variable substitution. This substitution is executed inside the double quotes and the square brackets, but NOT inside the curly braces. Example:
% set  my_message  "Hello!" 
% puts "I'd like to say : $my_message" 
[] Square brackets Same meaning as of the reverse-quote (`) for the UNIX shells. The string enclosed in brackets is replaced with the result of the execution of the string itself. Example:
% set  a  [ expr 10 + 10 ]

In the example, the command ``expr'' evaluates the three arguments as a mathematical expression. The resulting string becomes the second eargument of the ``set'' command.

\ Backslash The backslash can be used to access special characters ( \$, \[, \] ).
"" Double quote Group several characters into a single argument. Necessary every time an argument contains spaces, tabs or newlines.
Example: print a string with an embedded newline:
% puts "Hello world!
This is a wonderful day" 
Hello world!                            
This is a wonderful day 
{} Curly braces Group words into a single argument. Elements within the braces are not interpreted, so no variable or command substitution takes place.
Example:
% set HOME "/home/username" 
% puts {This won't be interpreted: $HOME }
This won't be interpreted: $HOME
              
% puts "While this will be substituted: $HOME"
While this will be substituted: /home/username

Simple Variable

Tcl provides a typeless kind of variable that can be used to store strings, numbers, lists and scripts:
  • Always stored as strings
  • No need for declarations
  • Created automatically when a value is assigned...
  • ... and removed with the ``unset'' command
  • Variable names are case sensitive
  • The value is assigned with the ``set'' command ...
  • ... and accessed using the dollar notation ($)
Examples:
% set a {this is a string}
% set n 9.1                                   ;# this is a string too !
% unset a n                                   ;# unset the two variables

When a variable name has to be concatenated with a letter, a number or an underscore (without spaces in between), the variable name has to be quoted with a pair of curly braces.

Example:
% set temperature 12.3 
% puts ${temperature}F   
12.3F

Associate Arrays

Tcl provides arrays, in the form of collection of elements. The complete name of an array element has two parts:
  1. The name of the array
  2. The name of the element within the array
Examples:
% set monthDays(January) 31 
% set monthDays(February) 28 
% set dummy(this-is-the entry-name) {any value, even $HOME} 
# note the dashes and the space between 'the' and 'entry' 
To access an element use the dollar notation:
% set Year(Jan)  31
% puts $Year(Jan)
But when the name of the element is a variable itself a double substitution is performed:
% set anArray(theElement) "the value"
% set foo    "theElement"
% puts $anArray($foo)
Spaces and commas in the element name are treated as part of the name.
Simulate multidimensional arrays, by concatenating multiple indices into a single element name. Examples:
% set calendar(monday,0800) {Breakfast with John}
% set calendar(sunday,1100) {Game: go bears!}
% set day sunday
% set time 110
% puts $calendar($day,$time)
Game: go bears!                               ;# output

The comma (,) is only a common way to separate the components of the element name. Every symbol between parentheses is interpreted as part of the element name. Do not add spaces!

Lists

A list is an ordered collection of elements. It is represented as a string with a particular structure. The simplest form of list is a sequence of strings separated by spaces. Lists can be nested, using curly braces as delimiters. The empty string is {}

Example:
% set L1 "a b c d"
% set L2 { 1 { a b } 2 { hello "a b" } }
% lindex $L2 1
a b
% lindex $L2 3
hello "a b"
% lindex [ lindex $L2 3 ] 1
a b
% lindex [ lindex [ lindex $L2 3 ] 0 ]
a
% lindex $L2 end
hello "a b"
The example shows the use of the lindex command to access the elements of a list.
  • lindex wants two (2) arguments: a list and a position
  • The index 0 corresponds to the first element
  • The "end'' symbol corresponds to the last element
  • The elements of a nested list (like L2) can be accessed using nested invocation of the lindex command (see: command substitution via square brackets)

Procedures

  • The definition of a new procedure is done with a command, called proc
  • It takes three (3) arguments: name, list of arguments, and body: proc name args body
Example:
% proc Fact { n } {
    if { $n <= 1 } { 
        return 1 
    }
    return [expr $n * [Fact [expr $n-1]]]
}

% Fact 4 
24 
  • More like functions in C, rather than Perl
  • Variables created inside a procedure are local to that procedure
  • To access global variables, use global keyword
  • Take arguments as any other Tcl command (i.e. space separated)
  • Return a string with the return command
  • The argument list is actually a list of lists...
  • ... each sublist has either one or two elements
  • In the latter case, the second argument is the default value
    Example:
    proc my_inc { value {increment 1} } {
        return [expr $value + $increment]
    }