| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993-1994 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH array n 8.3 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
array \- Manipulate array variables |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBarray \fIoption arrayName\fR ?\fIarg arg ...\fR? |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
This command performs one of several operations on the |
| 20 |
variable given by \fIarrayName\fR. |
| 21 |
Unless otherwise specified for individual commands below, |
| 22 |
\fIarrayName\fR must be the name of an existing array variable. |
| 23 |
The \fIoption\fR argument determines what action is carried |
| 24 |
out by the command. |
| 25 |
The legal \fIoptions\fR (which may be abbreviated) are: |
| 26 |
.TP |
| 27 |
\fBarray anymore \fIarrayName searchId\fR |
| 28 |
Returns 1 if there are any more elements left to be processed |
| 29 |
in an array search, 0 if all elements have already been |
| 30 |
returned. |
| 31 |
\fISearchId\fR indicates which search on \fIarrayName\fR to |
| 32 |
check, and must have been the return value from a previous |
| 33 |
invocation of \fBarray startsearch\fR. |
| 34 |
This option is particularly useful if an array has an element |
| 35 |
with an empty name, since the return value from |
| 36 |
\fBarray nextelement\fR will not indicate whether the search |
| 37 |
has been completed. |
| 38 |
.TP |
| 39 |
\fBarray donesearch \fIarrayName searchId\fR |
| 40 |
This command terminates an array search and destroys all the |
| 41 |
state associated with that search. \fISearchId\fR indicates |
| 42 |
which search on \fIarrayName\fR to destroy, and must have |
| 43 |
been the return value from a previous invocation of |
| 44 |
\fBarray startsearch\fR. Returns an empty string. |
| 45 |
.TP |
| 46 |
\fBarray exists \fIarrayName\fR |
| 47 |
Returns 1 if \fIarrayName\fR is an array variable, 0 if there |
| 48 |
is no variable by that name or if it is a scalar variable. |
| 49 |
.TP |
| 50 |
\fBarray get \fIarrayName\fR ?\fIpattern\fR? |
| 51 |
Returns a list containing pairs of elements. The first |
| 52 |
element in each pair is the name of an element in \fIarrayName\fR |
| 53 |
and the second element of each pair is the value of the |
| 54 |
array element. The order of the pairs is undefined. |
| 55 |
If \fIpattern\fR is not specified, then all of the elements of the |
| 56 |
array are included in the result. |
| 57 |
If \fIpattern\fR is specified, then only those elements whose names |
| 58 |
match \fIpattern\fR (using the matching rules of |
| 59 |
\fBstring match\fR) are included. |
| 60 |
If \fIarrayName\fR is not the name of an array variable, or if |
| 61 |
the array contains no elements, then an empty list is returned. |
| 62 |
If traces on the array modify the list of elements, the elements |
| 63 |
returned are those that exist both before and after the call to |
| 64 |
\fBarray get\fR. |
| 65 |
.TP |
| 66 |
\fBarray names \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR? |
| 67 |
Returns a list containing the names of all of the elements in |
| 68 |
the array that match \fIpattern\fR. \fIMode\fR may be one of |
| 69 |
\fB\-exact\fR, \fB\-glob\fR, or \fB\-regexp\fR. If specified, \fImode\fR |
| 70 |
designates which matching rules to use to match \fIpattern\fR against |
| 71 |
the names of the elements in the array. If not specified, \fImode\fR |
| 72 |
defaults to \fB\-glob\fR. See the documentation for \fBstring match\fR |
| 73 |
for information on glob style matching, and the documentation for |
| 74 |
\fBregexp\fR for information on regexp matching. |
| 75 |
If \fIpattern\fR is omitted then the command returns all of |
| 76 |
the element names in the array. If there are no (matching) elements |
| 77 |
in the array, or if \fIarrayName\fR is not the name of an array |
| 78 |
variable, then an empty string is returned. |
| 79 |
.TP |
| 80 |
\fBarray nextelement \fIarrayName searchId\fR |
| 81 |
Returns the name of the next element in \fIarrayName\fR, or |
| 82 |
an empty string if all elements of \fIarrayName\fR have |
| 83 |
already been returned in this search. The \fIsearchId\fR |
| 84 |
argument identifies the search, and must have |
| 85 |
been the return value of an \fBarray startsearch\fR command. |
| 86 |
Warning: if elements are added to or deleted from the array, |
| 87 |
then all searches are automatically terminated just as if |
| 88 |
\fBarray donesearch\fR had been invoked; this will cause |
| 89 |
\fBarray nextelement\fR operations to fail for those searches. |
| 90 |
.TP |
| 91 |
\fBarray set \fIarrayName list\fR |
| 92 |
Sets the values of one or more elements in \fIarrayName\fR. |
| 93 |
\fIlist\fR must have a form like that returned by \fBarray get\fR, |
| 94 |
consisting of an even number of elements. |
| 95 |
Each odd-numbered element in \fIlist\fR is treated as an element |
| 96 |
name within \fIarrayName\fR, and the following element in \fIlist\fR |
| 97 |
is used as a new value for that array element. |
| 98 |
If the variable \fIarrayName\fR does not already exist |
| 99 |
and \fIlist\fR is empty, |
| 100 |
\fIarrayName\fR is created with an empty array value. |
| 101 |
.TP |
| 102 |
\fBarray size \fIarrayName\fR |
| 103 |
Returns a decimal string giving the number of elements in the |
| 104 |
array. |
| 105 |
If \fIarrayName\fR is not the name of an array then 0 is returned. |
| 106 |
.TP |
| 107 |
\fBarray startsearch \fIarrayName\fR |
| 108 |
This command initializes an element-by-element search through the |
| 109 |
array given by \fIarrayName\fR, such that invocations of the |
| 110 |
\fBarray nextelement\fR command will return the names of the |
| 111 |
individual elements in the array. |
| 112 |
When the search has been completed, the \fBarray donesearch\fR |
| 113 |
command should be invoked. |
| 114 |
The return value is a |
| 115 |
search identifier that must be used in \fBarray nextelement\fR |
| 116 |
and \fBarray donesearch\fR commands; it allows multiple |
| 117 |
searches to be underway simultaneously for the same array. |
| 118 |
It is currently more efficient and easier to use either the \fBarray |
| 119 |
get\fR or \fBarray names\fR, together with \fBforeach\fR, to iterate |
| 120 |
over all but very large arrays. See the examples below for how to do |
| 121 |
this. |
| 122 |
.TP |
| 123 |
\fBarray statistics \fIarrayName\fR |
| 124 |
Returns statistics about the distribution of data within the hashtable |
| 125 |
that represents the array. This information includes the number of |
| 126 |
entries in the table, the number of buckets, and the utilization of |
| 127 |
the buckets. |
| 128 |
.TP |
| 129 |
\fBarray unset \fIarrayName\fR ?\fIpattern\fR? |
| 130 |
Unsets all of the elements in the array that match \fIpattern\fR (using the |
| 131 |
matching rules of \fBstring match\fR). If \fIarrayName\fR is not the name |
| 132 |
of an array variable or there are no matching elements in the array, no |
| 133 |
error will be raised. If \fIpattern\fR is omitted and \fIarrayName\fR is |
| 134 |
an array variable, then the command unsets the entire array. |
| 135 |
The command always returns an empty string. |
| 136 |
.SH EXAMPLES |
| 137 |
.CS |
| 138 |
\fBarray set\fR colorcount { |
| 139 |
red 1 |
| 140 |
green 5 |
| 141 |
blue 4 |
| 142 |
white 9 |
| 143 |
} |
| 144 |
|
| 145 |
foreach {color count} [\fBarray get\fR colorcount] { |
| 146 |
puts "Color: $color Count: $count" |
| 147 |
} |
| 148 |
\fB\(->\fR Color: blue Count: 4 |
| 149 |
Color: white Count: 9 |
| 150 |
Color: green Count: 5 |
| 151 |
Color: red Count: 1 |
| 152 |
|
| 153 |
foreach color [\fBarray names\fR colorcount] { |
| 154 |
puts "Color: $color Count: $colorcount($color)" |
| 155 |
} |
| 156 |
\fB\(->\fR Color: blue Count: 4 |
| 157 |
Color: white Count: 9 |
| 158 |
Color: green Count: 5 |
| 159 |
Color: red Count: 1 |
| 160 |
|
| 161 |
foreach color [lsort [\fBarray names\fR colorcount]] { |
| 162 |
puts "Color: $color Count: $colorcount($color)" |
| 163 |
} |
| 164 |
\fB\(->\fR Color: blue Count: 4 |
| 165 |
Color: green Count: 5 |
| 166 |
Color: red Count: 1 |
| 167 |
Color: white Count: 9 |
| 168 |
|
| 169 |
\fBarray statistics\fR colorcount |
| 170 |
\fB\(->\fR 4 entries in table, 4 buckets |
| 171 |
number of buckets with 0 entries: 1 |
| 172 |
number of buckets with 1 entries: 2 |
| 173 |
number of buckets with 2 entries: 1 |
| 174 |
number of buckets with 3 entries: 0 |
| 175 |
number of buckets with 4 entries: 0 |
| 176 |
number of buckets with 5 entries: 0 |
| 177 |
number of buckets with 6 entries: 0 |
| 178 |
number of buckets with 7 entries: 0 |
| 179 |
number of buckets with 8 entries: 0 |
| 180 |
number of buckets with 9 entries: 0 |
| 181 |
number of buckets with 10 or more entries: 0 |
| 182 |
average search distance for entry: 1.2 |
| 183 |
.CE |
| 184 |
.SH "SEE ALSO" |
| 185 |
list(n), string(n), variable(n), trace(n), foreach(n) |
| 186 |
.SH KEYWORDS |
| 187 |
array, element names, search |