XPS-Q8 Tcl Manual
⇒ var
set name
⇒ var
set $name
⇒ the value of var
This is a somewhat tricky example. In the last command, $name gets substituted with
var. Then, the set command returns the value of var, which is the value of
var. Nested set commands provide another way to achieve a level of indirection.
The last set command above can be written as follows:
set [set name]
⇒ the value of var
Using a variable to store the name of another variable may seem overly complex,
however, there are situations when it is very useful. There is even a special command,
upvar, that makes this sort of trick easier.
2.2.9.1 Funny Variable Names
The Tcl interpreter makes some assumptions about variable names that makes it easy to
embed variable references into other strings. By default, it will assume variable names
contain only letters, digits, and the underscore. The construct $foo.o represents a
concatenation of the value of foo and the literal “.o”.
If the variable reference is not delimited by punctuation or white space, then you can
use curly braces to explicitly delimit the variable name (e.g., ${x}). You can also use
this to reference variables with funny characters in their name, although generally the
use of funny variable names is not ideal. If you find yourself using funny variable
names, or computing the names of variables, then you may want to use the upvar
command.
Example 1–16: Embedded variable references.
set foo filename
set object $foo.o
⇒ filename.o
set a AAA
set b abc${a}def
⇒ abcAAAdef
set .o yuk!
set x ${.o}y
⇒ yuk!y
2.2.9.2 The unset Command
You can delete a variable with the unset command:
unset varName varName2 ...
Any number of variable names can be passed to the unset command. However,
unset will raise an error if a variable is not already defined.
EDH0307En1041 — 10/17
10