Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 8.  Tcl Arrays

Array Syntax

The index of an array is delimited by parentheses. The index can have any string value, and it can be the result of variable or command substitution. Array elements are defined with set:

set arr(index) value

The value of an array element is obtained with $ substitution:

set foo $arr(index)

Example 8-1 uses the loop variable value $i as an array index. It sets arr(x) to the product of 1 * 2 * ... * x:

Example 8-1 Using arrays.
set arr(0) 1
for {set i 1} {$i <= 10} {incr i} {
   set arr($i) [expr {$i * $arr([expr $i-1])}]

Complex Indices

An array index can be any string, like orange, 5, 3.1415, or foo,bar. The examples in this chapter, and in this book, often use indices that are pretty complex strings to create flexible data structures. As a rule of thumb, you can use any string for an index, but avoid using a string that contains spaces.


Parentheses are not a grouping mechanism.

The main Tcl parser does not know about array syntax. All the rules about grouping and substitution described in Chapter 1 are still the same in spite of the array syntax described here. Parentheses do not group like curly braces or quotes, which is why a space causes problems. If you have complex indices, use a comma to separate different parts of the index. If you use a space in an index instead, then you have a quoting problem. The space in the index needs to be quoted with a backslash, or the whole variable reference needs to be grouped:

set {arr(I'm asking for trouble)} {I told you so.}
set arr(I'm\ asking\ for\ trouble) {I told you so.}

If the array index is stored in a variable, then there is no problem with spaces in the variable's value. The following works well:

set index {I'm asking for trouble}
set arr($index) {I told you so.}

Array Variables

You can use an array element as you would a simple variable. For example, you can test for its existence with info exists, increment its value with incr, and append elements to it with lappend:

if {[info exists stats($event)]} {incr stats($event)}

You can delete an entire array, or just a single array element with unset. Using unset on an array is a convenient way to clear out a big data structure.

It is an error to use a variable as both an array and a normal variable. The following is an error:

set arr(0) 1
set arr 3
=> can't set "arr": variable is array

The name of the array can be the result of a substitution. This is a tricky situation, as shown in Example 8-2:

Example 8-2 Referencing an array indirectly.
set name TheArray
=> TheArray
set ${name}(xyz) {some value}
=> some value
set x $TheArray(xyz)
=> some value
set x ${name}(xyz)
=> TheArray(xyz)
set x [set ${name}(xyz)]
=> some value

A better way to deal with this situation is to use the upvar command, which is introduced on page 85. The previous example is much cleaner when upvar is used:

Example 8-3 Referencing an array indirectly using upvar.
set name TheArray
=> TheArray
upvar 0 $name a
set a(xyz) {some value}
=> some value
set x $TheArray(xyz)
=> some value

      Previous section   Next section