dict for

Runs a script for each key and value of a dict.

dict for {KEYVAR VALUEVAR} DICT BODY

Description

Runs BODY once for each key of DICT, in order, with the key in the variable KEYVAR and the value in VALUEVAR. break in BODY ends the loop, and continue goes on with the next key. An error in BODY ends the loop with that error.

The loop works on the value of DICT at its start, so a change of a variable in BODY does not change the keys of the loop. The result is an empty string.

Arguments

ArgumentTypeDefaultDescription
{KEYVAR VALUEVAR}list of two variable namesrequiredThe variables of the key and of the value.
DICTdictrequiredThe dict.
BODYscriptrequiredThe script to run for each key.

Returns

An empty string.

Examples

% dict for {k v} {a 1 b 2} {puts "$k=$v"}
a=1
b=2

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

% dict for {k v} {a 1 b 2} {if {$k eq "b"} break; puts $k}
a

Errors

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

% dict for {k v} {a 1} {error boom}
boom
-errorcode NONE

Facts

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

See also