Navigation Logo 2.2  Variables in Tcl Navigation Logo

 

 

In Tcl, variable names can be almost any string of characters. The one restriction, explained below in Regional Variables, is that a variable name cannot contain ::. However, taking full advantage of Tcl's free form variable names requires finesse and does not make for readable scripts. Accordingly, I suggest you begin your variables names with a letter and continue them with alphameric characters and the underscore character "_." (By alphameric I mean a letter or a digit.)

Remark

The restriction against including :: in a variable name was introduced with version 8.0. If you have an older script that happens to have :: in a variable name, the name will have to be changed.

Other than its name, the other important aspect of a variable is its value. Variable values can also be arbitrary strings. In fact, they are always either strings or one-dimensional arrays of strings – the only data types in Tcl are strings and one-dimensional arrays. You never declare a Tcl variable's type. The interpreter can infer that type from syntactical hints. You must, however, assign values to variables before you use them – there are no default initial values.

When numbers are the values of variables, they have their ASCII form and not one of the internal forms your computer uses for calculations.

Unlike most programming languages, in Tcl, when you write the name of a variable, you mean the name – not the value. That is why

X + 1
would be an error if you meant "add one to X." The reason is that "X" can only be the name of a variable, not its value. To represent the value of a variable, put a dollar sign in front of it, for example, $X.

Before each line of Tcl code is executed, something called variable substitution happens: the line is searched for dollar signs followed by variable names. Each such occurrence, for example, $X, is replaced with the value of the named variable. Only after this substitution process is complete is the command executed.

Two caveats:

  • Variable substitution cannot happen if there is no value associated with the variable – in that case you get an error.

  • After a $, which characters are considered to be part of a variable name? As many as possible – provided they are alphameric or an underscore. If this is a problem, and it sometimes is, then use curly brackets as shown in More about Substitution.

You already know that constructs like while which control the flow of execution are implemented as procedures. You might guess that variable assignment is too. You would be correct. The procedure is called set and here are examples of its use:

% set X 2
2
% set Y $X
2
As with many examples in this book, this one shows an interactive session. Lines beginning with the % prompt are command lines. Other lines are generated by Tcl in one of two ways: they show the return string from the command on the previous line or they are the result of a puts command.

In this example, the first statement assigns the value 2 to the variable named X and returns the value. The second statement assigns the value of the variable named X to the variable named Y and returns the value. Of course, when both statements have executed, the value of both variables is a string containing the numeric character 2.

By the way, Tcl handles return strings differently when executed interactively than when executed directly. Interactive execution works as shown with this example: return strings are printed in the command window. With direct execution, return strings are normally ignored. The exception is explained below in Command Substitution.

Remark

In this book, "a variable has a value V" and "a variable contains V" mean the same thing.

Exercise 2.2a

What are the values of X, Y, and Z after these statements have executed?
set X Y
set $X 2
set Z $X.TCL

Solution

Even though the set procedure returns the value that it assigns, this return value is ignored except when a command is executed in a context that requires a return value.

The set procedure can also be used with just one argument. In that case, the argument is viewed as a variable name and the value of that variable is returned. Thus, set X is similar to $X but they are appropriate in somewhat different contexts.

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.