Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/dict.n @ 37

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

added tcl to libs

File size: 13.5 KB
Line 
1'\"
2'\" Copyright (c) 2003 Donal K. Fellows
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: dict.n,v 1.18 2007/12/31 00:17:44 dkf Exp $
8'\"
9.so man.macros
10.TH dict n 8.5 Tcl "Tcl Built-In Commands"
11.BS
12'\" Note:  do not modify the .SH NAME line immediately below!
13.SH NAME
14dict \- Manipulate dictionaries
15.SH SYNOPSIS
16\fBdict \fIoption arg \fR?\fIarg ...\fR?
17.BE
18.SH DESCRIPTION
19.PP
20Performs one of several operations on dictionary values or variables
21containing dictionary values (see the \fBDICTIONARY VALUES\fR section
22below for a description), depending on \fIoption\fR.  The legal
23\fIoption\fRs (which may be abbreviated) are:
24.TP
25\fBdict append \fIdictionaryVariable key \fR?\fIstring ...\fR?
26This appends the given string (or strings) to the value that the given
27key maps to in the dictionary value contained in the given variable,
28writing the resulting dictionary value back to that variable.
29Non-existent keys are treated as if they map to an empty string.
30.TP
31\fBdict create \fR?\fIkey value ...\fR?
32Create a new dictionary that contains each of the key/value mappings
33listed as arguments (keys and values alternating, with each key being
34followed by its associated value.)
35.TP
36\fBdict exists \fIdictionaryValue key \fR?\fIkey ...\fR?
37This returns a boolean value indicating whether the given key (or path
38of keys through a set of nested dictionaries) exists in the given
39dictionary value. This returns a true value exactly when \fBdict
40get\fR on that path will succeed.
41.TP
42\fBdict filter \fIdictionaryValue filterType arg \fR?\fIarg ...\fR?
43This takes a dictionary value and returns a new dictionary that
44contains just those key/value pairs that match the specified filter
45type (which may be abbreviated.)  Supported filter types are:
46.RS
47.TP
48\fBdict filter \fIdictionaryValue \fBkey \fIglobPattern\fR
49The key rule only matches those key/value pairs whose keys match the
50given pattern (in the style of \fBstring match\fR.)
51.TP
52\fBdict filter \fIdictionaryValue \fBscript {\fIkeyVar valueVar\fB} \fIscript\fR
53The script rule tests for matching by assigning the key to the
54\fIkeyVar\fR and the value to the \fIvalueVar\fR, and then evaluating
55the given script which should return a boolean value (with the
56key/value pair only being included in the result of the \fBdict
57filter\fR when a true value is returned.)  Note that the first
58argument after the rule selection word is a two-element list.  If the
59\fIscript\fR returns with a condition of \fBTCL_BREAK\fR, no further
60key/value pairs are considered for inclusion in the resulting
61dictionary, and a condition of \fBTCL_CONTINUE\fR is equivalent to a false
62result. The key/value pairs are tested in the order in which the keys
63were inserted into the dictionary.
64.TP
65\fBdict filter \fIdictionaryValue \fBvalue \fIglobPattern\fR
66The value rule only matches those key/value pairs whose values match
67the given pattern (in the style of \fBstring match\fR.)
68.RE
69.TP
70\fBdict for {\fIkeyVar valueVar\fB} \fIdictionaryValue body\fR
71This command takes three arguments, the first a two-element list of
72variable names (for the key and value respectively of each mapping in
73the dictionary), the second the dictionary value to iterate across,
74and the third a script to be evaluated for each mapping with the key
75and value variables set appropriately (in the manner of \fBforeach\fR.)
76The result of the command is an empty string. If any evaluation of the
77body generates a \fBTCL_BREAK\fR result, no further pairs from the
78dictionary will be iterated over and the \fBdict for\fR command will
79terminate successfully immediately. If any evaluation of the body
80generates a \fBTCL_CONTINUE\fR result, this shall be treated exactly like a
81normal \fBTCL_OK\fR result. The order of iteration is the order in
82which the keys were inserted into the dictionary.
83.TP
84\fBdict get \fIdictionaryValue \fR?\fIkey ...\fR?
85Given a dictionary value (first argument) and a key (second argument),
86this will retrieve the value for that key. Where several keys are
87supplied, the behaviour of the command shall be as if the result of
88\fBdict get $dictVal $key\fR was passed as the first argument to
89\fBdict get\fR with the remaining arguments as second (and possibly
90subsequent) arguments. This facilitates lookups in nested
91dictionaries. For example, the following two commands are equivalent:
92.RS
93.CS
94dict get $dict foo bar spong
95dict get [dict get [dict get $dict foo] bar] spong
96.CE
97If no keys are provided, dict would return a list containing pairs of
98elements in a manner similar to \fBarray get\fR. That is, the first
99element of each pair would be the key and the second element would be
100the value for that key.
101
102It is an error to attempt to retrieve a value for a key that is not
103present in the dictionary.
104.RE
105.TP
106\fBdict incr \fIdictionaryVariable key \fR?\fIincrement\fR?
107This adds the given increment value (an integer that defaults to 1 if
108not specified) to the value that the given key maps to in the
109dictionary value contained in the given variable, writing the
110resulting dictionary value back to that variable. Non-existent keys
111are treated as if they map to 0. It is an error to increment a value
112for an existing key if that value is not an integer.
113.TP
114\fBdict info \fIdictionaryValue\fR
115This returns information (intended for display to people) about the
116given dictionary though the format of this data is dependent on the
117implementation of the dictionary. For dictionaries that are
118implemented by hash tables, it is expected that this will return the
119string produced by \fBTcl_HashStats\fR, similar to \fBarray info\fR.
120.TP
121\fBdict keys \fIdictionaryValue \fR?\fIglobPattern\fR?
122Return a list of all keys in the given dictionary value. If a pattern
123is supplied, only those keys that match it (according to the rules of
124\fBstring match\fR) will be returned. The returned keys will be in the
125order that they were inserted into the dictionary.
126.TP
127\fBdict lappend \fIdictionaryVariable key \fR?\fIvalue ...\fR?
128This appends the given items to the list value that the given key maps
129to in the dictionary value contained in the given variable, writing
130the resulting dictionary value back to that variable. Non-existent
131keys are treated as if they map to an empty list, and it is legal for
132there to be no items to append to the list. It is an error for the
133value that the key maps to to not be representable as a list.
134.TP
135\fBdict merge \fR?\fIdictionaryValue ...\fR?
136Return a dictionary that contains the contents of each of the
137\fIdictionaryValue\fR arguments.  Where two (or more) dictionaries
138contain a mapping for the same key, the resulting dictionary maps that
139key to the value according to the last dictionary on the command line
140containing a mapping for that key.
141.TP
142\fBdict remove \fIdictionaryValue \fR?\fIkey ...\fR?
143Return a new dictionary that is a copy of an old one passed in as
144first argument except without mappings for each of the keys listed.
145It is legal for there to be no keys to remove, and it also legal for
146any of the keys to be removed to not be present in the input
147dictionary in the first place.
148.TP
149\fBdict replace \fIdictionaryValue \fR?\fIkey value ...\fR?
150Return a new dictionary that is a copy of an old one passed in as
151first argument except with some values different or some extra
152key/value pairs added. It is legal for this command to be called with
153no key/value pairs, but illegal for this command to be called with a
154key but no value.
155.TP
156\fBdict set \fIdictionaryVariable key \fR?\fIkey ...\fR? \fIvalue\fR
157This operation takes the name of a variable containing a dictionary
158value and places an updated dictionary value in that variable
159containing a mapping from the given key to the given value. When
160multiple keys are present, this operation creates or updates a chain
161of nested dictionaries.
162.TP
163\fBdict size \fIdictionaryValue\fR
164Return the number of key/value mappings in the given dictionary value.
165.TP
166\fBdict unset \fIdictionaryVariable key \fR?\fIkey ...\fR?
167This operation (the companion to \fBdict set\fR) takes the name of a
168variable containing a dictionary value and places an updated
169dictionary value in that variable that does not contain a mapping for
170the given key. Where multiple keys are present, this describes a path
171through nested dictionaries to the mapping to remove. At least one key
172must be specified, but the last key on the key-path need not exist.
173All other components on the path must exist.
174.TP
175\fBdict update \fIdictionaryVariable key varName \fR?\fIkey varName ...\fR? \fIbody\fR
176Execute the Tcl script in \fIbody\fR with the value for each \fIkey\fR
177(as found by reading the dictionary value in \fIdictionaryVariable\fR)
178mapped to the variable \fIvarName\fR. There may be multiple
179\fIkey\fR/\fIvarName\fR pairs. If a \fIkey\fR does not have a mapping,
180that corresponds to an unset \fIvarName\fR. When \fIbody\fR
181terminates, any changes made to the \fIvarName\fRs is reflected back
182to the dictionary within \fIdictionaryVariable\fR (unless
183\fIdictionaryVariable\fR itself becomes unreadable, when all updates
184are silently discarded), even if the result of \fIbody\fR is an error
185or some other kind of exceptional exit. The result of \fBdict
186update\fR is (unless some kind of error occurs) the result of the
187evaluation of \fIbody\fR. Note that the mapping of values to variables
188does not use traces; changes to the \fIdictionaryVariable\fR's
189contents only happen when \fIbody\fR terminates.
190.TP
191\fBdict values \fIdictionaryValue \fR?\fIglobPattern\fR?
192Return a list of all values in the given dictionary value. If a
193pattern is supplied, only those values that match it (according to the
194rules of \fBstring match\fR) will be returned. The returned values
195will be in the order of that the keys associated with those values
196were inserted into the dictionary.
197.TP
198\fBdict with \fIdictionaryVariable \fR?\fIkey ...\fR? \fIbody\fR
199Execute the Tcl script in \fIbody\fR with the value for each key in
200\fIdictionaryVariable\fR mapped (in a manner similarly to \fBdict
201update\fR) to a variable with the same name. Where one or more
202\fIkey\fRs are available, these indicate a chain of nested
203dictionaries, with the innermost dictionary being the one opened out
204for the execution of \fIbody\fR. As with \fBdict update\fR, making
205\fIdictionaryVariable\fR unreadable will make the updates to the
206dictionary be discarded, and this also happens if the contents of
207\fIdictionaryVariable\fR are adjusted so that the chain of
208dictionaries no longer exists. The result of \fBdict with\fR is
209(unless some kind of error occurs) the result of the evaluation of
210\fIbody\fR. Note that the mapping of values to variables does not use
211traces; changes to the \fIdictionaryVariable\fR's contents only happen
212when \fIbody\fR terminates.
213.SH "DICTIONARY VALUES"
214Dictionaries are values that contain an efficient, order-preserving
215mapping from arbitrary keys to arbitrary values.
216Each key in the dictionary maps to a single value.
217They have a textual format that is exactly that of any list with an
218even number of elements, with each mapping in the dictionary being
219represented as two items in the list. When a command takes a
220dictionary and produces a new dictionary based on it (either returning
221it or writing it back into the variable that the starting dictionary
222was read from) the new dictionary will have the same order of keys,
223modulo any deleted keys and with new keys added on to the end.
224When a string is interpreted as a dictionary and it would otherwise
225have duplicate keys, only the last value for a particular key is used;
226the others are ignored, meaning that,
227.QW "apple banana"
228and
229.QW "apple carrot apple banana"
230are equivalent dictionaries (with different string representations).
231.SH EXAMPLES
232Constructing and using nested dictionaries:
233.CS
234# Data for one employee
235\fBdict set\fR employeeInfo 12345-A forenames "Joe"
236\fBdict set\fR employeeInfo 12345-A surname   "Schmoe"
237\fBdict set\fR employeeInfo 12345-A street "147 Short Street"
238\fBdict set\fR employeeInfo 12345-A city   "Springfield"
239\fBdict set\fR employeeInfo 12345-A phone  "555-1234"
240# Data for another employee
241\fBdict set\fR employeeInfo 98372-J forenames "Anne"
242\fBdict set\fR employeeInfo 98372-J surname   "Other"
243\fBdict set\fR employeeInfo 98372-J street "32995 Oakdale Way"
244\fBdict set\fR employeeInfo 98372-J city   "Springfield"
245\fBdict set\fR employeeInfo 98372-J phone  "555-8765"
246# The above data probably ought to come from a database...
247
248# Print out some employee info
249set i 0
250puts "There are [\fBdict size\fR $employeeInfo] employees"
251\fBdict for\fR {id info} $employeeInfo {
252   puts "Employee #[incr i]: $id"
253   \fBdict with\fR info {
254      puts "   Name: $forenames $surname"
255      puts "   Address: $street, $city"
256      puts "   Telephone: $phone"
257   }
258}
259# Another way to iterate and pick out names...
260foreach id [\fBdict keys\fR $employeeInfo] {
261   puts "Hello, [\fBdict get\fR $employeeInfo $id forenames]!"
262}
263.CE
264.PP
265A localizable version of \fBstring toupper\fR:
266.CS
267# Set up the basic C locale
268set capital [\fBdict create\fR C [\fBdict create\fR]]
269foreach c [split {abcdefghijklmnopqrstuvwxyz} ""] {
270   \fBdict set\fR capital C $c [string toupper $c]
271}
272
273# English locales can luckily share the "C" locale
274\fBdict set\fR capital en [\fBdict get\fR $capital C]
275\fBdict set\fR capital en_US [\fBdict get\fR $capital C]
276\fBdict set\fR capital en_GB [\fBdict get\fR $capital C]
277
278# ... and so on for other supported languages ...
279
280# Now get the mapping for the current locale and use it.
281set upperCaseMap [\fBdict get\fR $capital $env(LANG)]
282set upperCase [string map $upperCaseMap $string]
283.CE
284.SH "SEE ALSO"
285append(n), array(n), foreach(n), incr(n), list(n), lappend(n), set(n)
286.SH KEYWORDS
287dictionary, create, update, lookup, iterate, filter
Note: See TracBrowser for help on using the repository browser.