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

SEE ALSO

75       for(n), while(n), break(n), continue(n)
76
77

KEYWORDS

79       foreach, iteration, list, looping
80
81
82
83Tcl                                                                 foreach(n)
Impressum