Tcl array Command
last modified October 20, 2026
The Tcl array command provides operations for working with
associative arrays. Arrays in Tcl are collections of key-value pairs.
They are similar to dictionaries in other languages.
Basic Definition
Tcl arrays are associative arrays where keys can be arbitrary strings. They are different from traditional indexed arrays in other languages.
Syntax: array option arrayName ?arg ...?. The command takes
an operation name, array name, and optional arguments depending on the
operation.
Creating and Setting Array Elements
This example shows how to create an array and set its elements.
array set colors {
red #ff0000
green #00ff00
blue #0000ff
}
puts $colors(red)
puts $colors(green)
puts $colors(blue)
Here we create an array named colors with three elements.
The array set command initializes the array with key-value
pairs. We then access individual elements using the $array(key)
syntax.
Getting Array Size and Names
The array size and array names commands provide
information about an array.
array set fruits {
apple 10
orange 15
banana 20
}
puts "Number of fruits: [array size fruits]"
puts "Fruit names: [array names fruits]"
This code creates a fruits array and then displays its size and keys.
array size returns the number of elements, while
array names returns a list of all keys. The order of the keys is
not defined, so the output may differ from run to run; use
lsort when a stable order is needed.
Iterating Over Array Elements
The array get command converts an array to a list, which
can be used for iteration.
array set temperatures {
monday 72
tuesday 68
wednesday 75
}
foreach {day temp} [array get temperatures] {
puts "$day: $temp°F"
}
This example shows how to iterate through all elements of an array.
The array get command returns a flat list of key-value
pairs, which we process with foreach. The elements come in no
particular order.
Checking Array Existence
The array exists command checks if a variable is an array.
array set user {
name John
age 30
city {New York}
}
if {[array exists user]} {
puts "User array exists"
} else {
puts "User array does not exist"
}
This demonstrates how to verify if a variable is an array before attempting to use array operations on it. The command returns 1 if the variable is an array, 0 otherwise.
Searching Array Elements
The array names command accepts a glob pattern, which lets us
select the elements whose keys match it.
array set books {
tcl_programming 25
python_guide 30
tcl_cookbook 20
java_basics 15
}
set tcl_books [array names books *tcl*]
foreach book $tcl_books {
puts "$book: \$[set books($book)]"
}
This example searches for all books with "tcl" in their names. The
array names command with a glob pattern returns matching
keys (use the -regexp mode for regular expressions). We then
display these books with their prices.
Unsetting Array Elements
The array unset command removes the elements whose keys match a glob
pattern, or the entire array when no pattern is given.
array set config {
debug 1
logfile /var/log/app.log
timeout 30
}
array unset config logfile
puts "Remaining config: [array get config]"
array unset config
puts "Array exists after unset: [array exists config]"
Here we first remove just the logfile element from the
array, then we remove the entire array. The second argument is a glob pattern,
not a plain key: array unset config l* would remove every element
whose key begins with "l". To remove exactly one element, use
unset config(logfile).
Best Practices
- Initialization: Use
array setfor bulk initialization. - Checking: Use
array existsto tell an array from a scalar variable or a missing one. - Patterns: Use glob patterns with
array namesfor searching. - Order: Do not rely on the order of
array namesorarray get; sort the keys when order matters. - Naming: Choose descriptive array names for better code clarity.
This tutorial covered the Tcl array command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.