beetlebug▾
/

array forTcl 9.1 only

Runs a script for each element of an array, with its name and value in two variables.

array for {KEYVAR VALUEVAR} ARRAYNAME BODY

Description

Sets KEYVAR to the name and VALUEVAR to the value of each element, in the order in which the elements were made, and runs BODY each time. break ends the loop and continue goes to the next element. The first argument is a list of exactly two variable names.

The loop goes through the names that the array has at the start. A value that BODY changes does not change the value of VALUEVAR of that turn.

The subcommand is in Tcl 9.1 only.

Arguments

ArgumentTypeDefaultDescription
KEYVARvariable namerequiredThe variable of the name of the element: the first element of the list.
VALUEVARvariable namerequiredThe variable of the value of the element: the second element of the list.
ARRAYNAMEvariable namerequiredThe array.
BODYscriptrequiredThe script to run for each element.

Returns

An empty string.

Examples

% array set f {a 1 b 2}; array for {k v} f {lappend out $k=$v}; set out
a=1 b=2

% array for {k v} f {if {$k eq "a"} break}; set k
a

% array for {k v} f {set f($k) 9; lappend seen $v}; set seen
1 2

Errors

% array for {k v} nope {}
"nope" isn't an array
-errorcode TCL LOOKUP ARRAY nope

% array for k f {}
must have two variable names
-errorcode NONE

% array for {k v} f
wrong # args: should be "array for {key value} arrayName body"
-errorcode TCL WRONGARGS

Facts

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

See also