array(n)
π array(n) β the Tcl Built-In Commands era π
okay bestie, so array is a Tcl built-in command and it is literally the whole toolkit for messing with array variables. she's giving main character energy in every Tcl script ever. let's get into it π«Ά
β¨ NAME β¨
array β manipulate array variables πΈ
β¨ SYNOPSIS β¨
array option arrayName ?arg arg ...?
β¨ DESCRIPTION β¨
so this command does one of several operations on the variable named by arrayName. unless a specific subcommand below says otherwise, arrayName has to be the name of an existing array variable. the option argument decides which action actually happens. the legal options are below, and yes they may be abbreviated π
array anymorearrayName searchId β returns1if there are any more elements left to be processed in an array search, and0if all elements have already been returned. searchId says which search on arrayName to check, and it must have been the return value from a previous invocation ofarray startsearch. this one is particularly useful when an array has an element with an empty name, because the return value fromarray nextelementwon't tell you whether the search is done. πarray defaultsubcommand arrayName args... β manages the default value of the array. arrays initially have no default value, but this command lets you set one; the default value gets returned when reading from an element of the array arrayName if the read would otherwise result in an error. note that this may cause theappend,dict,incrandlappendcommands to change their behavior in relation to non-existing array elements. π
the subcommand argument controls exactly which operation gets performed on the default value of arrayName. the supported subcommands are:
array default existsarrayName β returns a boolean value indicating whether a default value has been set for the array arrayName. returns a false value if arrayName does not exist. raises an error if arrayName is an existing variable that is not an array. β¨array default getarrayName β returns the current default value for the array arrayName. raises an error if arrayName is an existing variable that is not an array, or if arrayName is an array without a default value. πarray default setarrayName value β sets the default value for the array arrayName to value. returns the empty string. raises an error if arrayName is an existing variable that is not an array, or if arrayName is an illegal name for an array. if arrayName does not currently exist, it gets created as an empty array as well as having its default value set. πarray default unsetarrayName β removes the default value for the array arrayName and returns the empty string. does nothing if arrayName does not have a default value. raises an error if arrayName is an existing variable that is not an array. π
array donesearcharrayName searchId β terminates an array search and destroys all the state associated with that search. searchId says which search on arrayName to destroy, and it must have been the return value from a previous invocation ofarray startsearch. returns an empty string. bye bye search state πarray existsarrayName β returns1if arrayName is an array variable,0if there is no variable by that name or if it's a scalar variable. vibe check: is it an array or not π₯array for{keyVariable valueVariable} arrayName body β the first argument is a two element list of variable names for the key and value of each entry in the array. the second argument is the array name to iterate over. the third argument is the body to execute for each key and value returned. the ordering of the returned keys is undefined. if an array element is deleted or a new array element is inserted during thearray forprocess, the command will terminate with an error. β οΈarray getarrayName ?pattern? β returns a list containing pairs of elements. the first element in each pair is the name of an element in arrayName, and the second element of each pair is the value of the array element. the order of the pairs is undefined. if pattern is not specified, then all of the elements of the array are included in the result. if pattern is specified, then only those elements whose names match pattern (using the matching rules ofstring match) are included. if arrayName is not the name of an array variable, or if the array contains no elements, then an empty list is returned. if traces on the array modify the list of elements, the elements returned are those that exist both before and after the call toarray get. πarray namesarrayName ?mode? ?pattern? β returns a list containing the names of all of the elements in the array that match pattern. mode may be one of-exact,-glob, or-regexp. if specified, mode designates which matching rules to use to match pattern against the names of the elements in the array. if not specified, mode defaults to-glob. see the documentation forstring matchfor info on glob style matching, and the documentation forregexpfor info on regexp matching. if pattern is omitted then the command returns all of the element names in the array. if there are no (matching) elements in the array, or if arrayName is not the name of an array variable, then an empty string is returned. π¦array nextelementarrayName searchId β returns the name of the next element in arrayName, or an empty string if all elements of arrayName have already been returned in this search. the searchId argument identifies the search, and must have been the return value of anarray startsearchcommand.
β οΈ warning: if elements are added to or deleted from the array, then all searches are automatically terminated just as ifarray donesearchhad been invoked; this will causearray nextelementoperations to fail for those searches. π
array setarrayName list β sets the values of one or more elements in arrayName. list must have a form like that returned byarray get, consisting of an even number of elements. each odd-numbered element in list is treated as an element name within arrayName, and the following element in list is used as a new value for that array element. if the variable arrayName does not already exist and list is empty, arrayName is created with an empty array value. β¨array sizearrayName β returns a decimal string giving the number of elements in the array. if arrayName is not the name of an array then0is returned. π’array startsearcharrayName β initializes an element-by-element search through the array given by arrayName, such that invocations of thearray nextelementcommand will return the names of the individual elements in the array. when the search has been completed, thearray donesearchcommand should be invoked. the return value is a search identifier that must be used inarray nextelementandarray donesearchcommands; it allows multiple searches to be underway simultaneously for the same array. it is currently more efficient and easier to use eitherarray getorarray names, together withforeach, to iterate over all but very large arrays. see the examples below for how to do this πarray statisticsarrayName β returns statistics about the distribution of data within the hashtable that represents the array. this information includes the number of entries in the table, the number of buckets, and the utilization of the buckets. πarray unsetarrayName ?pattern? β unsets all of the elements in the array that match pattern (using the matching rules ofstring match). if arrayName is not the name of an array variable or there are no matching elements in the array, no error will be raised. if pattern is omitted and arrayName is an array variable, then the command unsets the entire array. the command always returns an empty string. π§Ή
β¨ EXAMPLES β¨
okay so here's the classic colorcount setup, she's iconic:
array set colorcount {
red 1
green 5
blue 4
white 9
}
iterating with array get + foreach (the efficient way, bestie):
foreach {color count} [array get colorcount] {
puts "Color: $color Count: $count"
}
β Color: blue Count: 4
Color: white Count: 9
Color: green Count: 5
Color: red Count: 1
same thing but with array names:
foreach color [array names colorcount] {
puts "Color: $color Count: $colorcount($color)"
}
β Color: blue Count: 4
Color: white Count: 9
Color: green Count: 5
Color: red Count: 1
and sorted with lsort because sometimes we want order in our lives:
foreach color [lsort [array names colorcount]] {
puts "Color: $color Count: $colorcount($color)"
}
β Color: blue Count: 4
Color: green Count: 5
Color: red Count: 1
Color: white Count: 9
and the hashtable tea β:
array statistics colorcount
β 4 entries in table, 4 buckets
number of buckets with 0 entries: 1
number of buckets with 1 entries: 2
number of buckets with 2 entries: 1
number of buckets with 3 entries: 0
number of buckets with 4 entries: 0
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 0
average search distance for entry: 1.2
β¨ SEE ALSO β¨
list(n), string(n), variable(n), trace(n), foreach(n) β go read these next, they're the friend group π«Ά
β¨ KEYWORDS β¨
array, element names, search π
and that's the whole page, bestie!! no cap, every flag and every caveat is right here π
this was array(n) from the Tcl Built-In Commands manual, Tcl version 9.0 β mwah, slay, periodt πβ¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1993-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms