Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/array.n @ 33

Last change on this file since 33 was 25, checked in by landauf, 16 years ago

added tcl to libs

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