Tcl Tutorial Lesson 14

Tcl Data Structures 101 - The list

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:

  • by setting a variable to be a list of values
   set lst {{item 1} {item 2} {item 3}}
  • with the split command:
   set lst [split "item 1.item 2.item 3" "."]
  • with the list command.
   set lst [list "item 1" "item 2" "item 3"]

An individual list member can be accessed with the lindex command.

The brief description of these commands is:

list ?arg1? ?arg2? ... ?argN?
makes a list of the arguments
split string ?splitChars?
splits the string into a list of items wherever the splitChars occur in the code. SplitChars defaults to being whitespace. Note that if there are two or more splitChars then each one will be used individually to split the string. In other words: split "1234567" "36" would return the following list: {12 45 7}.
lindex list index
Returns the index'th item from the list. If the list is a list of other lists, you can use several indices - lindex listOfLists index1 index2 ...

Note: lists start from 0, not 1, so the first item is at index 0, the second item is at index 1, and so on.

llength list
Returns the number of elements in a list.

The items in list can be iterated through using the foreach command:

    foreach varName list body

The foreach command will execute the body code one time for each list item in list. On each pass, varName will contain the value of the next list item.

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 {
        ...
    }

or:

    foreach {a b} $listOfAB {
        ...
    }

Furthermore, lists can be nested:

    set nestedList {
        {1 2 3}
        {A B C D}
        {xyz 33 1A}
    }

    puts "The last element from the second sublist is:"
    puts "    [lindex $nestedList 1 end]"

producing:

The last element from the second sublist is: D

Examples

set x "a b c d e f g h"
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
}
set i 0
foreach {a b} $x {
    puts "Pair $i in list x: $a, $b"
    incr i
}

 Resulting output
Item at index 2 of the list {a b c d e f g h} is: c

We celebrate on the 4'th day of the 7'th month

A command resembles: puts {arg 2 is 7 4 1776}

a is item number 0 in list x
b is item number 1 in list x
c is item number 2 in list x
d is item number 3 in list x
e is item number 4 in list x
f is item number 5 in list x
g is item number 6 in list x
h is item number 7 in list x
Pair 0 in list x: a, b
Pair 1 in list x: c, d
Pair 2 in list x: e, f
Pair 3 in list x: g, h