Version 6 of foreach

Updated 2002-01-23 15:52:36

foreach - Iterate over all elements in one or more lists

 http://purl.org/tcl/home/man/tcl8.4/TclCmd/foreach.htm

 foreach varname list body 
 foreach varlist1 list1 ?varlist2 list2 ...? body  

Synopsis

The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script. For each element of list (in order from first to last), foreach assigns the contents of the element to varname as if the lindex command had been used to extract the element, then calls the Tcl interpreter to execute body.

foreach varlist list body

Occasionally developers are surprised to discover that if list is modified within the confines of the body's code, varname never reflects the change. This is due to the tcl parser having a fixed copy of the elements of list in memory and not referring to the actual variable during the assignment of values to varname during the loop construct.

foreach varlist1 list1 ?varlist2 list2 ... varlistn listn? body

In the general case there can be more than one value list (e.g., list1 and list2), and each value list can be associated with a list of loop variables (e.g., varlist1 and varlist2). During each iteration of the loop the variables of each varlist are assigned consecutive values from the corresponding list. Values in each list are used in order from first to last, and each value is used exactly once. The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements.

The break and continue statements may be invoked inside body, with the same effect as in the for command. Foreach returns an empty string. (From: TclHelp)


It is a popular idiom to use the varlist assignments to split up a list. The body in such cases would just be break to stop foreach running into a second round:

 foreach {first second third} $list break ;# RS

You might as well swap variables with that:

 foreach {a b} [list $b $a] break

Get every second element of a list with the following ("-" being a fancy variable name that indicates it will not be used):

 foreach {- i} $list {...}

'Foreach' is also a convenient way to work with Tcl's associative arrays. For instance, you can save an array to a file as a Tcl script by using:

 foreach { key value } [array get myArray] { 
     puts $myFile [list set myArray($key) $value]
 }

Of course, you could have written: If you want the keys alphabetized, then

 foreach { key } [lsort -dictionary [array names myArray]] {
     puts $myFile [list set myArray($key) $myArray($key)]
 }
 puts $myFile [list array set myArray [array get myArray]]
but when editing such a file, the lack of line endings might give your text
but that style creates scripts which cause trouble in many text
editors.
----
Applying 'foreach' to the result of the 'bbox' command in a canvas is often a convenient way to create canvas layouts.  The following script gives the idea.
 grid [canvas .c -width 400 -height 300 -bg gray85]
 set i [.c create text 200 50 -text "One" -anchor n]
 foreach item { Two Three Four } {
     foreach { - - - y } [.c bbox $i] break
     set nexty [expr { $y + 50 }]
     .c create line 200 $y 200 $nexty -arrow last
     set i [.c create text 200 $nexty -text $item -anchor n]
 }

One-time loop: You can use foreach also for a loop that runs exactly once, and you're not interested in the iterator, but get the side effect that you can "jump out" of the body with break or continue (seen in a c.l.t post by Bruce Hartweg):

 foreach _ _ {
    if {$somecondition} break
    # some other processing
    if {$someothercondition} break
    # still more processing

}

 }

Helps you avoid nested if structures. RS is not sure whether to recommend this style.. but proof again that you can do more with Tcl than you'd imagine...;-) Well, hell, then add some sugar:

 interp alias {} breakable {} foreach _ _
 breakable { ;# or 'fragile'?
    if {$somecondition} break
    # some other processing
    if {$someothercondition} break
    # still more processing

}

 }

 interp alias {} make {} foreach
 make or break {

}

 }

See also for, while .


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command