Assigning values to variables
In Tcl, everything may be represented as a string, although internally it may be represented as a list, integer, double, or other type, in order to make the language fast.
The assignment command in Tcl is
set is called with two
arguments, as in:
set fruit Cauliflower
it places the second argument (
Cauliflower) in the memory space referenced by the
first argument (
Set always returns the contents of the variable
named in the first argument. Thus, when
set is called with two arguments, it places the
second argument in the memory space referenced by the first
argument and then returns the second argument. In the above
example, for instance, it would return "Cauliflower", without the
The first argument to a
can be either a single word, like
pi , or it can be a member of an array.
Arrays will be discussed in greater detail later, for the time
being just remember that many data can be collected under a single
variable name, and an individual datum can be accessed by its index
within that array. Indexing into an array in Tcl is handled by
putting the index within parentheses after the name of the
Set can also be invoked with only
one argument. When called with just one argument, it will return
the contents of that argument.
Here's a summary of the
valueis specified, then the contents of the variable
varNameare set equal to
varNameconsists only of alphanumeric characters, and no parentheses, it is a scalar variable.
varNamehas the form
varName(index), it is a member of an associative array.
If you look at the example code, you'll notice that in the
set command the first argument is typed with
only its name, but in the
statement the argument is preceded with a $.
The dollar sign tells Tcl to use the value of the variable - in
Tcl passes data to subroutines either by name or by value. Commands that don't change the contents of a variable usually have their arguments passed by value. Commands that do change the value of the data must have the data passed by name.
set X "This is a string" set Y 1.24 puts $X puts $Y puts "..............................." set label "The value in Y is: " puts "$label $Y"