Tcl Syntax
- Every script is a sequence of commands separated by newlines or semicolons.
- All commands are formed by one or more words separated by spaces or
tabs.
CMD arg arg arg ...
- The first word is always the name of the command.
- The remaining words are the arguments.
- Even ``if'', ``while'', ``for'', ``foreach'' and ``switch'', and the definition of new procedures obey this rule.
- If the first character of a command is a ``#'' the text that follows (up to the next newline) is treated as a comment.
% 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
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:
|
[] | 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:
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:
|
{} | Curly braces | Group words into a single argument. Elements within the
braces are not interpreted, so no variable or command
substitution takes place. Example:
|
Simple Variable
- 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 ($)
% 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.
% set temperature 12.3
% puts ${temperature}F
12.3F
Associate Arrays
- The name of the array
- The name of the element within the array
% 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'
% set Year(Jan) 31
% puts $Year(Jan)
% 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.% 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 {}
% 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"
- 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
% 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] }