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

SubcommandSummary
dict createMakes a dict from keys and values.
dict getGives the value at a path of keys.
dict getdefGives the value at a path of keys, or a default when a key is missing. Tcl 9.1 only
dict getwithdefaultGives the value at a path of keys, or a default: the same command as dict getdef. Tcl 9.1 only
dict existsTests whether a path of keys exists.
dict setSets the value at a path of keys in the dict of a variable.
dict unsetRemoves the key at the end of a path from the dict of a variable.
dict keysGives the keys of a dict, or the keys that match a glob pattern.
dict valuesGives the values of a dict, or the values that match a glob pattern.
dict sizeGives the count of keys of a dict.
dict forRuns a script for each key and value of a dict.
dict mapMakes a dict with the same keys and the results of a script as values.
dict filterGives the keys and values of a dict that pass a test.
dict mergeJoins dicts into one, with the value of a later dict for a key in both.
dict removeGives a copy of a dict with some keys removed.
dict replaceGives a copy of a dict with new values for some keys.
dict incrAdds an integer to the value of a key in the dict of a variable.
dict appendAdds text to the end of the value of a key in the dict of a variable.
dict lappendAdds elements to the list value of a key in the dict of a variable.
dict updateRuns a script with variables linked to keys of the dict of a variable.
dict withRuns a script with a variable for each key of the dict of a variable.
dict infoGives 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

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also