The list is the basic Tcl data structure. A list is simply an ordered collection of stuff; numbers, words, strings, or other lists. Even commands in Tcl are just lists in which the first list entry is the name of a proc, and subsequent members of the list are the arguments to the proc.
Lists can be created in several ways: <DT>by setting a variable to be a list of values</dt> <DD> set lst {{item 1} {item 2} {item 3}}</dd> <DT>with the split command</dt> <DD> set lst split "item 1.item 2.item 3" "."</dd> <DT>with the list command.</dt> <DD> set lst list "item 1" "item 2" "item 3"</dd>
An individual list member can be accessed with the lindex command.
The brief description of these commands is:
list ?arg1? ?arg2? ... ?argN? </dt>
lindex list index </dt>
llength list </dt>
In reality, the above form of foreach is the simple form, but the command is quite powerful. It will allow you to take more than one variable at a time from the list: foreach {a b} $listofpairs { ... }. You can even take a variable at a time from multiple lists! For example: foreach a $listOfA b $listOfB { ... }
set x "a b c" puts "Item at index 2 of the list {$x} is: [lindex $x 2]\n" set y [split 7/4/1776 "/"] puts "We celebrate on the [lindex $y 1]'th day of the [lindex $y 0]'th month\n" set z [list puts "arg 2 is $y" ] puts "A command resembles: $z\n" set i 0 foreach j $x { puts "$j is item number $i in list x" incr i }