lmap
Maps a command over one or more lists, executing a body script for each iteration and collecting results into an accumulator list.
Syntax
tcl
lmap varList list ?varList list ...? commandParameters
- varList: Variable name(s) for iteration (must be non-empty)
- list: List to iterate over
- command: Body script executed for each iteration
Supported Features
- Basic single-variable iteration:
lmap varname list body- Iterates through a list, assigning each element to a variable - Multiple variables per list:
lmap {a b} list body- Assigns consecutive list elements to multiple variables per iteration - Multiple varlist/list pairs:
lmap var1 list1 var2 list2 body- Supports parallel iteration over multiple lists - Empty value padding: When a list runs out of elements, remaining variables receive empty strings
- break statement: Exits the loop early, returning accumulated results so far
- continue statement: Skips appending the current body result to the accumulator and proceeds to the next iteration
- Error propagation: Errors in the body script are propagated appropriately
- return propagation: Return statements in the body are propagated
Examples
Transform each element
Output
Filter and transform
Output
Multiple variables per iteration
Output
Multiple lists
Output
Early exit with break
Output
Implementation Notes
- The iteration count is calculated as
ceiling(listLen / numVars)and uses the maximum across all varlist/list pairs, ensuring all values from all lists are used. - When
breakorcontinueis invoked, the body does not complete normally and the result is not appended to the accumulator list. - Variables are set in the current scope following standard TCL semantics.
