dict map

Makes a dict with the same keys and the results of a script as values.

dict map {KEYVAR VALUEVAR} DICT BODY

Description

Runs BODY once for each key of DICT, as dict for does, and gives a new dict: each key with the result of BODY as its value. A change of KEYVAR in BODY does not change the key of the result.

continue in BODY leaves the key out of the result. break ends the loop and gives an empty dict.

Arguments

ArgumentTypeDefaultDescription
{KEYVAR VALUEVAR}list of two variable namesrequiredThe variables of the key and of the value.
DICTdictrequiredThe dict.
BODYscriptrequiredThe script that gives each new value.

Returns

The new dict.

Examples

% dict map {k v} {a 1 b 2} {expr {$v * 10}}
a 10 b 20

% dict map {k v} {a 1 b 2 c 3} {if {$k eq "b"} continue; set v}
a 1 c 3

% dict map {k v} {a 1 b 2 c 3} {if {$k eq "b"} break; set v}

% dict map {k v} {a 1} {set k z; set v}
a 1

Errors

% dict map {k} {a 1} {}
must have exactly two variable names
-errorcode NONE

% dict map {k v} {a 1} {error inner}
inner
-errorcode NONE

Differences from Tcl

Facts

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

See also