dict with

Runs a script with a variable for each key of the dict of a variable.

dict with VAR ?KEY ...? BODY

Description

Makes a variable for each key of the dict in the variable VAR, with the value of the key, and runs BODY. After BODY, the value of each of those variables goes back to its key, and a variable with no value removes its key. A new variable of BODY does not go into the dict.

With a path of KEY arguments, the dict at the end of the path is the one with the variables, and the write back goes to that place.

The write back happens also when BODY ends with an error. The result is the result of BODY.

Arguments

ArgumentTypeDefaultDescription
VARvariable namerequiredThe variable with the dict.
KEYstringnoneA key of a path to a nested dict.
BODYscriptrequiredThe script.

Returns

The result of BODY.

Examples

% set d {a 1 b 2}
> dict with d {incr a; set b x}
> set d
a 2 b x

% set d {p {x 1 y 2}}
> dict with d p {set x 5}
> set d
p {x 5 y 2}

% set d {a 1}
> dict with d {set z 9}
> set d
a 1

% set d {a 1}
> catch {dict with d {set a 2; error oops}}
> set d
a 2

Errors

% set d {p {x 1}}
> dict with d q {set y 2}
key "q" not known in dictionary
-errorcode NONE

% unset -nocomplain nod
> dict with nod {}
can't read "nod": no such variable
-errorcode NONE

Facts

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

See also