lmap
Runs a script for each element of one or more lists, and gives a list of the results.
lmap VARLIST LIST ?VARLIST LIST ...? BODY
Description
Runs BODY for each pass as foreach does, and gives a list with the result of BODY of each pass, in order. The variables, the passes, and the lists in step follow the rules of foreach.
continue in BODY leaves the pass out of the result, and break ends the loop with the results so far. An error in BODY ends the loop with that error. An empty BODY gives an empty element for each pass, and an empty LIST gives an empty list.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| VARLIST | list of variable names | required | The variables of one list, at least one name. |
| LIST | list | required | A list to go through. |
| BODY | script | required | The script that gives each element of the result. |
Returns
The list of the results of BODY.
Examples
% lmap x {1 2 3} {expr {$x * $x}}
1 4 9
% lmap x {1 2 3 4} {if {$x % 2} continue; set x}
2 4
% lmap x {1 2 3 4} {if {$x == 3} break; set x}
1 2
% lmap {a b} {1 2 3 4 5} {list $a $b}
{1 2} {3 4} {5 {}}
% lmap x {1 2} y {a b c} {list $x $y}
{1 a} {2 b} {{} c}
% lmap x {1 2} {}
{} {}
Errors
% lmap {} {1} {set x}
lmap varlist is empty
-errorcode NONE
% lmap x {1 2} {error oops}
oops
-errorcode NONE
% lmap x {1}
wrong # args: should be "lmap varList list ?varList list ...? command"
-errorcode TCL WRONGARGS
Differences from Tcl
- An empty VARLIST has
-errorcode NONE, where Tcl 8.6.16 and 9.0.4 giveTCL OPERATION LMAP NEEDVARS(issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1