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

NAME

8       foreach - Iterate over all elements in one or more lists
9

SYNOPSIS

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

DESCRIPTION

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

EXAMPLES

40       This  loop  prints  every  value in a list together with the square and
41       cube of the value:
42
43              set values {1 3 5 7 2 4 6 8}  ;# Odd numbers first, for fun!
44              puts "Value\tSquare\tCube"    ;# Neat-looking header
45              foreach x $values {           ;# Now loop and print...
46                  puts " $x\t [expr {$x**2}]\t [expr {$x**3}]"
47              }
48
49       The following loop uses i and j as loop variables to iterate over pairs
50       of elements of a single list.
51
52              set x {}
53              foreach {i j} {a b c d e f} {
54                  lappend x $j $i
55              }
56              # The value of x is "b a d c f e"
57              # There are 3 iterations of the loop.
58
59       The next loop uses i and j to iterate over two lists in parallel.
60
61              set x {}
62              foreach i {a b c} j {d e f g} {
63                  lappend x $i $j
64              }
65              # The value of x is "a d b e c f {} g"
66              # There are 4 iterations of the loop.
67
68       The two forms are combined in the following example.
69
70              set x {}
71              foreach i {a b c} {j k} {d e f g} {
72                  lappend x $i $j $k
73              }
74              # The value of x is "a d e b f g c {} {}"
75              # There are 3 iterations of the loop.
76
77

SEE ALSO

79       for(n), while(n), break(n), continue(n)
80
81

KEYWORDS

83       foreach, iteration, list, loop
84
85
86
87Tcl                                                                 foreach(n)
Impressum