array(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ‘‘ 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 anymore arrayName searchId β€” returns 1 if there are any more elements left to be processed in an array search, and 0 if 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 of array startsearch. this one is particularly useful when an array has an element with an empty name, because the return value from array nextelement won't tell you whether the search is done. πŸ’€
  • array default subcommand 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 the append, dict, incr and lappend commands 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 exists arrayName β€” 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 get arrayName β€” 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 set arrayName 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 unset arrayName β€” 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 donesearch arrayName 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 of array startsearch. returns an empty string. bye bye search state πŸ‘‹
  • array exists arrayName β€” returns 1 if arrayName is an array variable, 0 if 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 the array for process, the command will terminate with an error. ⚠️
  • array get arrayName ?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 of string 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 to array get. 🌈
  • array names arrayName ?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 for string match for info on glob style matching, and the documentation for regexp for 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 nextelement arrayName 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 an array startsearch command.
⚠️ warning: if elements are added to or deleted from the array, then all searches are automatically terminated just as if array donesearch had been invoked; this will cause array nextelement operations to fail for those searches. πŸ’€
  • array set arrayName list β€” sets the values of one or more elements in arrayName. list must have a form like that returned by array 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 size arrayName β€” returns a decimal string giving the number of elements in the array. if arrayName is not the name of an array then 0 is returned. πŸ”’
  • array startsearch arrayName β€” initializes an element-by-element search through the array given by arrayName, such that invocations of the array nextelement command will return the names of the individual elements in the array. when the search has been completed, the array donesearch command should be invoked. the return value is a search identifier that must be used in array nextelement and array donesearch commands; it allows multiple searches to be underway simultaneously for the same array. it is currently more efficient and easier to use either array get or array names, together with foreach, to iterate over all but very large arrays. see the examples below for how to do this πŸ‘‡
  • array statistics arrayName β€” 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 unset arrayName ?pattern? β€” unsets all of the elements in the array that match pattern (using the matching rules of string 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