Assigning list elements to variables
At this point, we have created, concatenated, joined and appended to our list. Next we need to know how to assign the elements of a list to variables to be able to access the individual elements. To accomplish this, Tcl provides the lassign
command. The syntax is as follows:
lassign list variable1 variable2 …
How to do it…
In the following example, we will assign the elements of our list to a set of variables and print out the values contained within the variables. Return values from the commands are provided for clarity. Enter the following command:
% lassign {John Mary Bill Tom Fred} 1 2 3 Tom Fred % puts "$1 $2 $3" John Mary Bill
How it works…
The lassign
command accepts a list as the first argument and assigns the elements to the variables given in the following arguments. If there are more variables than list elements, they will contain empty strings. If there are more elements than variables, a list of unassigned elements will be returned, as in the...