1dict(n)                      Tcl Built-In Commands                     dict(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       dict - Manipulate dictionaries
9

SYNOPSIS

11       dict option arg ?arg ...?
12_________________________________________________________________
13

DESCRIPTION

15       Performs  one  of  several operations on dictionary values or variables
16       containing dictionary values (see the DICTIONARY VALUES  section  below
17       for  a description), depending on option.  The legal options (which may
18       be abbreviated) are:
19
20       dict append dictionaryVariable key ?string ...?
21              This appends the given string (or strings) to the value that the
22              given key maps to in the dictionary value contained in the given
23              variable, writing the resulting dictionary value  back  to  that
24              variable.   Non-existent  keys  are treated as if they map to an
25              empty string.
26
27       dict create ?key value ...?
28              Create a new dictionary that contains each of the key/value map‐
29              pings  listed  as  arguments  (keys and values alternating, with
30              each key being followed by its associated value.)
31
32       dict exists dictionaryValue key ?key ...?
33              This returns a boolean value indicating whether  the  given  key
34              (or path of keys through a set of nested dictionaries) exists in
35              the given dictionary value. This returns a  true  value  exactly
36              when dict get on that path will succeed.
37
38       dict filter dictionaryValue filterType arg ?arg ...?
39              This  takes a dictionary value and returns a new dictionary that
40              contains just those key/value pairs  that  match  the  specified
41              filter  type (which may be abbreviated.)  Supported filter types
42              are:
43
44              dict filter dictionaryValue key globPattern
45                     The key rule only matches  those  key/value  pairs  whose
46                     keys  match  the  given  pattern  (in the style of string
47                     match.)
48
49              dict filter dictionaryValue script {keyVar valueVar} script
50                     The script rule tests for matching by assigning  the  key
51                     to  the  keyVar  and  the value to the valueVar, and then
52                     evaluating the given script which should return a boolean
53                     value (with the key/value pair only being included in the
54                     result of the dict filter when a true value is returned.)
55                     Note  that  the  first  argument after the rule selection
56                     word is a two-element list.  If the script returns with a
57                     condition  of  TCL_BREAK,  no further key/value pairs are
58                     considered for inclusion in the resulting dictionary, and
59                     a  condition  of  TCL_CONTINUE  is  equivalent to a false
60                     result. The key/value pairs are tested in  the  order  in
61                     which the keys were inserted into the dictionary.
62
63              dict filter dictionaryValue value globPattern
64                     The  value  rule only matches those key/value pairs whose
65                     values match the given pattern (in the  style  of  string
66                     match.)
67
68       dict for {keyVar valueVar} dictionaryValue body
69              This command takes three arguments, the first a two-element list
70              of variable names (for the key and value  respectively  of  each
71              mapping  in  the dictionary), the second the dictionary value to
72              iterate across, and the third a script to be evaluated for  each
73              mapping  with  the key and value variables set appropriately (in
74              the manner of foreach.)  The result of the command is  an  empty
75              string.  If  any  evaluation  of  the body generates a TCL_BREAK
76              result, no further pairs from the dictionary  will  be  iterated
77              over  and the dict for command will terminate successfully imme‐
78              diately. If any evaluation of the body generates a  TCL_CONTINUE
79              result,  this  shall  be  treated  exactly  like a normal TCL_OK
80              result. The order of iteration is the order in  which  the  keys
81              were inserted into the dictionary.
82
83       dict get dictionaryValue ?key ...?
84              Given  a  dictionary  value  (first  argument) and a key (second
85              argument), this will retrieve the value for that key. Where sev‐
86              eral keys are supplied, the behaviour of the command shall be as
87              if the result of dict get $dictVal $key was passed as the  first
88              argument to dict get with the remaining arguments as second (and
89              possibly subsequent)  arguments.  This  facilitates  lookups  in
90              nested dictionaries. For example, the following two commands are
91              equivalent:
92                     dict get $dict foo bar spong
93                     dict get [dict get [dict get $dict foo] bar] spong
94              If no keys are provided, dict would  return  a  list  containing
95              pairs of elements in a manner similar to array get. That is, the
96              first element of each pair would be the key and the second  ele‐
97              ment would be the value for that key.
98
99              It  is an error to attempt to retrieve a value for a key that is
100              not present in the dictionary.
101
102       dict incr dictionaryVariable key ?increment?
103              This adds the given increment value (an integer that defaults to
104              1  if  not specified) to the value that the given key maps to in
105              the dictionary value contained in the  given  variable,  writing
106              the  resulting dictionary value back to that variable. Non-exis‐
107              tent keys are treated as if they map to 0. It  is  an  error  to
108              increment  a  value  for an existing key if that value is not an
109              integer.
110
111       dict info dictionaryValue
112              This returns information (intended for display to people)  about
113              the given dictionary though the format of this data is dependent
114              on the implementation of the dictionary. For  dictionaries  that
115              are  implemented  by  hash tables, it is expected that this will
116              return the string produced by Tcl_HashStats,  similar  to  array
117              info.
118
119       dict keys dictionaryValue ?globPattern?
120              Return  a  list  of all keys in the given dictionary value. If a
121              pattern is supplied, only those keys that match it (according to
122              the  rules  of string match) will be returned. The returned keys
123              will be in the order that they were inserted  into  the  dictio‐
124              nary.
125
126       dict lappend dictionaryVariable key ?value ...?
127              This  appends  the  given items to the list value that the given
128              key maps to in the dictionary value contained in the given vari‐
129              able,  writing the resulting dictionary value back to that vari‐
130              able. Non-existent keys are treated as if they map to  an  empty
131              list,  and it is legal for there to be no items to append to the
132              list. It is an error for the value that the key maps to  to  not
133              be representable as a list.
134
135       dict merge ?dictionaryValue ...?
136              Return  a  dictionary  that contains the contents of each of the
137              dictionaryValue arguments.  Where  two  (or  more)  dictionaries
138              contain  a  mapping  for  the same key, the resulting dictionary
139              maps that key to the value according to the last  dictionary  on
140              the command line containing a mapping for that key.
141
142       dict remove dictionaryValue ?key ...?
143              Return  a  new dictionary that is a copy of an old one passed in
144              as first argument except without mappings for each of  the  keys
145              listed.   It  is legal for there to be no keys to remove, and it
146              also legal for any of the keys to be removed to not  be  present
147              in the input dictionary in the first place.
148
149       dict replace dictionaryValue ?key value ...?
150              Return  a  new dictionary that is a copy of an old one passed in
151              as first argument except with  some  values  different  or  some
152              extra  key/value pairs added. It is legal for this command to be
153              called with no key/value pairs, but illegal for this command  to
154              be called with a key but no value.
155
156       dict set dictionaryVariable key ?key ...? value
157              This operation takes the name of a variable containing a dictio‐
158              nary value and places an updated dictionary value in that  vari‐
159              able containing a mapping from the given key to the given value.
160              When multiple  keys  are  present,  this  operation  creates  or
161              updates a chain of nested dictionaries.
162
163       dict size dictionaryValue
164              Return  the number of key/value mappings in the given dictionary
165              value.
166
167       dict unset dictionaryVariable key ?key ...?
168              This operation (the companion to dict set) takes the name  of  a
169              variable  containing  a  dictionary  value and places an updated
170              dictionary value in that variable that does not contain  a  map‐
171              ping  for  the  given key. Where multiple keys are present, this
172              describes a path through nested dictionaries to the  mapping  to
173              remove.  At least one key must be specified, but the last key on
174              the key-path need not exist.  All other components on  the  path
175              must exist.
176
177       dict update dictionaryVariable key varName ?key varName ...? body
178              Execute  the  Tcl script in body with the value for each key (as
179              found by reading the  dictionary  value  in  dictionaryVariable)
180              mapped  to  the variable varName. There may be multiple key/var‐
181              Name pairs. If a key does not have a mapping,  that  corresponds
182              to  an  unset varName. When body terminates, any changes made to
183              the varNames is reflected back to the dictionary within  dictio‐
184              naryVariable  (unless  dictionaryVariable itself becomes unread‐
185              able, when all updates are  silently  discarded),  even  if  the
186              result  of  body  is  an error or some other kind of exceptional
187              exit. The result of dict update is (unless some  kind  of  error
188              occurs) the result of the evaluation of body. Note that the map‐
189              ping of values to variables does not use traces; changes to  the
190              dictionaryVariable's contents only happen when body terminates.
191
192       dict values dictionaryValue ?globPattern?
193              Return  a list of all values in the given dictionary value. If a
194              pattern is supplied, only those values that match it  (according
195              to  the  rules  of  string match) will be returned. The returned
196              values will be in the order of that  the  keys  associated  with
197              those values were inserted into the dictionary.
198
199       dict with dictionaryVariable ?key ...? body
200              Execute  the  Tcl  script in body with the value for each key in
201              dictionaryVariable mapped (in a manner similarly to dict update)
202              to  a  variable  with  the same name. Where one or more keys are
203              available, these indicate a chain of nested  dictionaries,  with
204              the innermost dictionary being the one opened out for the execu‐
205              tion of body. As with  dict  update,  making  dictionaryVariable
206              unreadable will make the updates to the dictionary be discarded,
207              and this also happens if the contents of dictionaryVariable  are
208              adjusted so that the chain of dictionaries no longer exists. The
209              result of dict with is (unless some kind of  error  occurs)  the
210              result  of the evaluation of body. Note that the mapping of val‐
211              ues to variables does not use traces; changes to the dictionary‐
212              Variable's contents only happen when body terminates.
213

DICTIONARY VALUES

215       Dictionaries  are  values  that  contain an efficient, order-preserving
216       mapping from arbitrary keys to arbitrary values.  Each key in the  dic‐
217       tionary  maps  to  a  single value.  They have a textual format that is
218       exactly that of any list with an even number  of  elements,  with  each
219       mapping  in  the dictionary being represented as two items in the list.
220       When a command takes a dictionary and produces a new  dictionary  based
221       on  it  (either  returning it or writing it back into the variable that
222       the starting dictionary was read from) the new dictionary will have the
223       same  order of keys, modulo any deleted keys and with new keys added on
224       to the end.  When a string is interpreted as a dictionary and it  would
225       otherwise have duplicate keys, only the last value for a particular key
226       is used; the others are  ignored,  meaning  that,  “apple  banana”  and
227       “apple carrot apple banana” are equivalent dictionaries (with different
228       string representations).
229

EXAMPLES

231       Constructing and using nested dictionaries:
232              # Data for one employee
233              dict set employeeInfo 12345-A forenames "Joe"
234              dict set employeeInfo 12345-A surname   "Schmoe"
235              dict set employeeInfo 12345-A street "147 Short Street"
236              dict set employeeInfo 12345-A city   "Springfield"
237              dict set employeeInfo 12345-A phone  "555-1234"
238              # Data for another employee
239              dict set employeeInfo 98372-J forenames "Anne"
240              dict set employeeInfo 98372-J surname   "Other"
241              dict set employeeInfo 98372-J street "32995 Oakdale Way"
242              dict set employeeInfo 98372-J city   "Springfield"
243              dict set employeeInfo 98372-J phone  "555-8765"
244              # The above data probably ought to come from a database...
245
246              # Print out some employee info
247              set i 0
248              puts "There are [dict size $employeeInfo] employees"
249              dict for {id info} $employeeInfo {
250                 puts "Employee #[incr i]: $id"
251                 dict with info {
252                    puts "   Name: $forenames $surname"
253                    puts "   Address: $street, $city"
254                    puts "   Telephone: $phone"
255                 }
256              }
257              # Another way to iterate and pick out names...
258              foreach id [dict keys $employeeInfo] {
259                 puts "Hello, [dict get $employeeInfo $id forenames]!"
260              }
261
262       A localizable version of string toupper:
263              # Set up the basic C locale
264              set capital [dict create C [dict create]]
265              foreach c [split {abcdefghijklmnopqrstuvwxyz} ""] {
266                 dict set capital C $c [string toupper $c]
267              }
268
269              # English locales can luckily share the "C" locale
270              dict set capital en [dict get $capital C]
271              dict set capital en_US [dict get $capital C]
272              dict set capital en_GB [dict get $capital C]
273
274              # ... and so on for other supported languages ...
275
276              # Now get the mapping for the current locale and use it.
277              set upperCaseMap [dict get $capital $env(LANG)]
278              set upperCase [string map $upperCaseMap $string]
279

SEE ALSO

281       append(n), array(n), foreach(n), incr(n), list(n), lappend(n), set(n)
282

KEYWORDS

284       dictionary, create, update, lookup, iterate, filter
285
286
287
288Tcl                                   8.5                              dict(n)
Impressum