We already saw one form of substitution: variable substitution by putting
in front of a variable. Another form of substitution is
command substitution. Suppose you want to give to some variable a value
that's the result of some
computation: that's not possible with the constructs described so far:
% set myvar 3 * 27 wrong # args: should be "set varName ?newValue?" % set myvar 3*27 3*27 % set myvar expr 3*27 wrong # args: should be "set varName ?newValue?"The first and third try will fail because the number of arguments to
set
is wrong. The second try is not so strange when you remember that
Tcl only knows strings. What you want to do here is to substitute the value
of a computation (for example expr 3*27
) for one of the arguments.
This is done by using square brackets ('[
' and ']
'):
% set myvar [ expr 3*27 ] 81 % puts myvar 81
Tcl offers a couple of other forms of substitution, but these are not described here.