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

Get Practical Programming in Tcl & Tk, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.