Node:Variables (Tcl), Next:, Previous:Syntax (Tcl), Up:Tcl



Variables

This has been dealt with implicitly above. A variable has a name and a value. A name can be any string whatsoever, as can its value.

For example,

set "Old King Cole" "merry soul"

sets the value of the variable named Old King Cole to the value merry soul. Variable names can also be numbers:

set 123 "one two three"

sets the variable with name 123 to the value one two three. In general, it is better to use the usual conventions -- start with a letter then follow with a combination of letters, digits, and underscores -- when giving variables names to avoid confusion.

Array variables are also available in Tcl. These are denoted by an array name followed by an array index enclosed in round brackets. As an example:

set fred(one) 1
set fred(two) 2

will set the variable fred(one) to the value 1 and fred(two) to the value 2.

Tcl arrays are associative arrays in that both the array name and the array index can be arbitrary strings. This also makes multidimensional arrays possible if the index contains a comma:

set fred(one,two) 12

It is cheating in that the array is not stored as a multidimensional array with a pair of indices, but as a linear array with a single index that happens to contain a comma.