TCL Lists
Lists in Tcl provide a simple means by which to group collections of items, and deal with the collection as a single entity. When needed, the single items in the group can be accessed individually. Lists are represented in Tcl as strings with a specified format. As such, they can be used in any place where strings are normally allowed. The elements of a list are also strings, and therefore any form of data that can be represented by a string can be included in a list (allowing lists to be nested within one another).
The following examples will illustrates many important list commands:
Example 6.1:
set simple_list "John Joe Mary Susan" puts [lindex $simple_list 0] puts [lindex $simple_list 2]
Output:
John
Mary
Example 6.1 creates a simple list of four elements, each of which consists of one word. The lindex command
is then used to extract two of the elements in the list: the 0th element and the 2nd element. Note that list indexing is zero-based. It is also
important to see that the lindex command, along with most other list commands, takes an actual list as
its first argument, not the name of a variable containing a list. Thus the value of the variable "simple_list" is substitued into the lindex command.
Example 6.2:
set simple_list2 "Mike Sam Heather Jennifer" set compound_list [list $simple_list $simple_list2] puts $compound_list puts [llength $compound_list]
Output:
{John Joe Mary Susan} {Mike Sam Heather Jennifer}
2
Example 6.2 is a continuation of Example 6.1, and assumes the variable "simple_list" (created in Example 6.1) still exists. In this
example, a new variable called "simple_list2" is created, and assigned the value of another simple four-element list. A compound list
is then formed by using the list command, which simply forms a list from its arguments. The list
command ensures that proper list structure is observed, even when its arguments themselves are lists, or other complex structures. Displaying
the value of "compound_list" shows that it is a list of two elements, each of which is itself a list of four elements. The llength
command is used to obtain the length of the list, "compound_list", which is 2 in this case.
This example highlights two ways in which to create lists in Tcl: by explicitly listing the elements within quotes, and by using the list
command. Explicitly listing the elements works well when each of the elements is a single word. However, if the elements contain whitespaces, then maintaining
proper list structure becomes a little more tricky. For these cases, the list command proves very useful.
Example 6.3:
set mylist "Mercury Venus Mars" puts $mylist set mylist [linsert $mylist 2 Earth] puts $mylist lappend mylist Jupiter puts $mylist
Output:
Mercury Venus Mars
Mercury Venus Earth Mars
Mercury Venus Earth Mars Jupiter
In example 6.3, a simple list of 3 items is created, and assigned to the variable "mylist". The linsert
command is then used to insert a new item into this list. Note that, as with the llength command,
the linsert command takes an actual list as its first argument, not the name of a variable containing
a list. The linsert command returns a list that is the same as the list it was passed, except that
the specified item is inserted in the appropriate position. This return value needs to be assigned back to the variable "mylist" in
order for the list stored in that variable to change.
One list command that does not behave in this way is the lappend
command. It takes the name of a variable as its first argument, and appends its subsequent arguments onto the list stored in that variable. Thus the
value of the variable is modified directly. Understanding the difference between the way the lappend
command works, and the way that commands such as linsert work is fundamental to using lists correctly.