1lmap(n)                      Tcl Built-In Commands                     lmap(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       lmap  -  Iterate  over  all  elements  in one or more lists and collect
9       results
10

SYNOPSIS

12       lmap varname list body
13       lmap varlist1 list1 ?varlist2 list2 ...? body
14______________________________________________________________________________
15

DESCRIPTION

17       The lmap command implements a loop where the loop variable(s)  take  on
18       values  from  one or more lists, and the loop returns a list of results
19       collected from each iteration.
20
21       In the simplest case there is one loop variable, varname, and one list,
22       list,  that is a list of values to assign to varname. The body argument
23       is a Tcl script. For each element of  list  (in  order  from  first  to
24       last),  lmap  assigns  the contents of the element to varname as if the
25       lindex command had been used to extract the element, then calls the Tcl
26       interpreter  to  execute  body. If execution of the body completes nor‐
27       mally then the result of the body is appended to an  accumulator  list.
28       lmap returns the accumulator list.
29
30       In  the general case there can be more than one value list (e.g., list1
31       and list2), and each value list can be associated with a list  of  loop
32       variables  (e.g.,  varlist1 and varlist2). During each iteration of the
33       loop the variables of each varlist are assigned consecutive values from
34       the  corresponding  list.  Values  in  each list are used in order from
35       first to last, and each value is used exactly once. The total number of
36       loop  iterations  is large enough to use up all the values from all the
37       value lists. If a value list does not contain enough elements for  each
38       of  its loop variables in each iteration, empty values are used for the
39       missing elements.
40
41       The break and continue statements may be invoked inside body, with  the
42       same effect as in the for and foreach commands. In these cases the body
43       does not complete normally and the result is not appended to the  accu‐
44       mulator list.
45

EXAMPLES

47       Zip lists together:
48
49              set list1 {a b c d}
50              set list2 {1 2 3 4}
51              set zipped [lmap a $list1 b $list2 {list $a $b}]
52              # The value of zipped is "{a 1} {b 2} {c 3} {d 4}"
53
54       Filter a list to remove odd values:
55
56              set values {1 2 3 4 5 6 7 8}
57              proc isEven {n} {expr {($n % 2) == 0}}
58              set goodOnes [lmap x $values {expr {
59                  [isEven $x] ? $x : [continue]
60              }}]
61              # The value of goodOnes is "2 4 6 8"
62
63       Take a prefix from a list based on the contents of the list:
64
65              set values {8 7 6 5 4 3 2 1}
66              proc isGood {counter} {expr {$n > 3}}
67              set prefix [lmap x $values {expr {
68                  [isGood $x] ? $x : [break]
69              }}]
70              # The value of prefix is "8 7 6 5 4"
71

SEE ALSO

73       break(n), continue(n), for(n), foreach(n), while(n)
74

KEYWORDS

76       foreach, iteration, list, loop, map
77
78
79
80Tcl                                                                    lmap(n)
Impressum