foreach(n)
π foreach(n) π
okay bestie, welcome to the foreach page β it's a built-in Tcl command and it's literally the moment your loop game levels all the way up. iykyk. β¨
πΈ NAME πΈ
foreach β iterate over all elements in one or more lists π¦
π SYNOPSIS π
foreach varname list body
foreach varlist1 list1 ?varlist2 list2 ...? body
π DESCRIPTION π
so. the foreach command is out here implementing a loop where your loop variable(s) pick up values from one or more lists. that's the whole vibe. π
in the simplest case you've got one loop variable, varname, and one list, list β and list is just a list of values you're assigning to varname. then body is a Tcl script. for each element of list (in order, first to last, no skipping, no shuffling), foreach assigns the contents of that element to varname as if the lindex command had been used to extract the element, and then it calls the Tcl interpreter to execute body. slay. π₯
now the general case, and this is where it gets iconic: there can be more than one value list (like list1 and list2), and each value list can be paired up with its own list of loop variables (like varlist1 and varlist2). during every single iteration of the loop, the variables of each varlist get 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 β no double dipping bestie. π
the total number of loop iterations is large enough to use up all the values from all the value lists. and if a value list doesn't have enough elements for each of its loop variables in each iteration? empty values get used for the missing elements. (yes, you'll see {} in the examples below and that's literally it doing its thing. π«Ά)
the break and continue statements may be invoked inside body, with the same effect as in the for command. and foreach returns an empty string. that's it, that's the return. π
β¨ EXAMPLES β¨
this loop prints every value in a list together with the square and the cube of the value. cute little math flex:
set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun!
puts "Value\tSquare\tCube" ;# Neat-looking header
foreach x $values { ;# Now loop and print...
puts " $x\t [expr {$x**2}]\t [expr {$x**3}]"
}
the following loop uses i and j as loop variables to iterate over pairs of elements of a single list. two variables, one list, pairs for days:
set x {}
foreach {i j} {a b c d e f} {
lappend x $j $i
}
# The value of x is "b a d c f e"
# There are 3 iterations of the loop.
the next loop uses i and j to iterate over two lists in parallel. two lists, two variables, holding hands. π
set x {}
foreach i {a b c} j {d e f g} {
lappend x $i $j
}
# The value of x is "a d b e c f {} g"
# There are 4 iterations of the loop.
and the two forms are combined in the following example. bestie this one is the final boss:
set x {}
foreach i {a b c} {j k} {d e f g} {
lappend x $i $j $k
}
# The value of x is "a d e b f g c {} {}"
# There are 3 iterations of the loop.
π SEE ALSO π
π KEYWORDS π
foreachiterationlistloop
aaaand that's the page!! π¦ this one's a Tcl built-in, it lives under foreach(n), and ngl it ate. π
periodt. go loop something cute bestie, you've got this. ππΈβ¨
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms