dict update

Runs a script with variables linked to keys of the dict of a variable.

dict update VAR KEY VARNAME ?KEY VARNAME ...? BODY

Description

Sets each VARNAME to the value of its KEY in the dict of the variable VAR, runs BODY, and then writes each VARNAME back to its key. A VARNAME of a missing key has no value at the start. A VARNAME with no value at the end removes its key from the dict.

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.
KEYstringrequiredA key of the dict.
VARNAMEvariable namerequiredThe variable for the key before it.
BODYscriptrequiredThe script.

Returns

The result of BODY.

Examples

% set d {a 1 b 2}
> dict update d a x b y {incr x; set y z}
> set d
a 2 b z

% set d {a 1}
> dict update d q v {set v new}
> set d
a 1 q new

% set d {a 1}
> dict update d a v {unset v}
> set d

% set d {a 1}
> dict update d a v {string length done}
4

Errors

% dict update d a v b {}
wrong # args: should be "dict update dictVarName key varName ?key varName ...? script"
-errorcode TCL WRONGARGS

Facts

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

See also