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
| Argument | Type | Default | Description |
|---|---|---|---|
| {KEYVAR VALUEVAR} | list of two variable names | required | The variables of the key and of the value. |
| DICT | dict | required | The dict. |
| BODY | script | required | The 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
- In Tcl 8.6.16 and 9.0.4 the key of the result is the value of KEYVAR after BODY:
dict map {k v} {a 1} {set k z; set v}givesz 1there (issue 660).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1