dict
The commands for dicts: lists of keys and values with each key once.
dict SUBCOMMAND ?ARG ...?
Description
The commands for dicts. A dict is a list with an even count of elements: each key followed by its value. Each key is in a dict once, and the dict keeps the order in which the keys came in. A change of the value of a key keeps its place.
Each argument that is a dict must be a valid list with an even count of elements. In a list with a key twice, the last value is the one of the key, at the place of the first one.
A path of keys goes into nested dicts: dict get $d a b is the value of b in the dict that is the value of a. The subcommands with VAR in their synopsis change the dict in a variable and give the new dict. The others read a dict value and leave each variable as it is.
The first argument names the subcommand, and a unique prefix of its name is enough.
Subcommands
| Subcommand | Summary |
|---|---|
dict create | Makes a dict from keys and values. |
dict get | Gives the value at a path of keys. |
dict getdef | Gives the value at a path of keys, or a default when a key is missing. Tcl 9.1 only |
dict getwithdefault | Gives the value at a path of keys, or a default: the same command as dict getdef. Tcl 9.1 only |
dict exists | Tests whether a path of keys exists. |
dict set | Sets the value at a path of keys in the dict of a variable. |
dict unset | Removes the key at the end of a path from the dict of a variable. |
dict keys | Gives the keys of a dict, or the keys that match a glob pattern. |
dict values | Gives the values of a dict, or the values that match a glob pattern. |
dict size | Gives the count of keys of a dict. |
dict for | Runs a script for each key and value of a dict. |
dict map | Makes a dict with the same keys and the results of a script as values. |
dict filter | Gives the keys and values of a dict that pass a test. |
dict merge | Joins dicts into one, with the value of a later dict for a key in both. |
dict remove | Gives a copy of a dict with some keys removed. |
dict replace | Gives a copy of a dict with new values for some keys. |
dict incr | Adds an integer to the value of a key in the dict of a variable. |
dict append | Adds text to the end of the value of a key in the dict of a variable. |
dict lappend | Adds elements to the list value of a key in the dict of a variable. |
dict update | Runs a script with variables linked to keys of the dict of a variable. |
dict with | Runs a script with a variable for each key of the dict of a variable. |
dict info | Gives text for people with the count of entries of a dict. |
Returns
The result of the subcommand.
Examples
% set d [dict create text milk done 0]
text milk done 0
% dict get $d text
milk
% dict set d done 1
text milk done 1
% dict exists $d tags
0
% dict merge {a 1 b 2} {b 3}
a 1 b 3
% dict filter {a 1 b 2 c 3} value {[13]}
a 1 c 3
Errors
% dict get {a 1} b
key "b" not known in dictionary
-errorcode NONE
# Tcl 9.1 only
% dict nope
unknown or ambiguous subcommand "nope": must be append, create, exists, filter, for, get, getdef, getwithdefault, incr, info, keys, lappend, map, merge, remove, replace, set, size, unset, update, values, or with
-errorcode NONE
# Tcl 8.6 only
% dict nope
unknown or ambiguous subcommand "nope": must be append, create, exists, filter, for, get, incr, info, keys, lappend, map, merge, remove, replace, set, size, unset, update, values, or with
-errorcode NONE
# A value that is not a valid list.
% dict size "a \{"
unmatched open brace in dict
-errorcode TCL VALUE DICTIONARY BRACE
Tcl 8.6 and Tcl 9.1
- Tcl 9.1 adds
dict getdefand its other namedict getwithdefault.
Differences from Tcl
- Many errors have
-errorcode NONEin ztcl, where Tcl 8.6.16 and 9.0.4 give a code such asTCL LOOKUP DICTorTCL VALUE DICTIONARY(issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1