foreach − Iterate over all elements in one or more lists


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


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.

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.


This loop prints every value in a list together with the square and cube of the value:

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.

set x {}
{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.

set x {}
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.

The two forms are combined in the following example.

set x {}
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.


for(n), while(n), break(2), continue(n)


foreach, iteration, list, loop

More Linux Commands

vdprintf(3) - print to a file descriptor - Linux man page...
vdprintf.3 - The functions dprintf() and vdprintf() (as found in the glibc2 library) are exact analogs of fprintf(3) and vfprintf(3), except that they output to

ip(8) - show / manipulate routing, devices, policy routing a
COMMAND Specifies the action to perform on the object. The set of possible actions depends on the object type. It is possible to add, delete and show or list...

setreuid(2) - set real and/or effective user or group ID....
setreuid() sets real and effective user IDs of the calling process. Supplying a value of -1 for either the real or effective user ID forces the system to leave

connect(2) - initiate a connection on a socket (Man Page)...
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlen argument specifies the siz

pdf2ps(1) - Ghostscript PDF to PostScript translator........
pdf2ps uses gs(1) to convert the Adobe Portable Document Format (PDF) file input.pdf to PostScript(tm) in Normally the output is allowed to use PostS

pecho_wchar(3ncurses) - create and display curses pads......
The newpad routine creates and returns a pointer to a new pad data structure with the given number of lines, nlines, and columns, ncols. A pad is like a window,

XkbAllocGeomKeyAliases(3) - Allocate geometry key aliases...
XkbAllocGeomKeyAliases.3 - Xkb provides a number of functions to allocate and free subcomponents of a keyboard geometry. Use these functions to create or modify

__fwriting(3) - interfaces to stdio FILE structure (ManPage)
Solaris introduced routines to allow portable access to the internals of the FILE structure, and glibc also implemented these. The __fbufsize() function returns

btrfs-map-logical(8) map btrfs logical extent to physical ex
btrfs-map-logical can be used to find out what the physical offsets are on the mirrors, the result is dumped into stdout in default. Mainly used for debug purpo

cpp-4.6(1) - The C Preprocessor (Commands - Linux man page)
cpp-4.6.1 - The C preprocessor, often known as cpp, is a macro processor that is used automatically by the C compiler to transform your program before compilati

Tcl_NewUnicodeObj(3) - manipulate Tcl objects as strings....
The procedures described in this manual entry allow Tcl objects to be manipulated as string values. They use the internal representation of the object to store

XauUnlockAuth(3) - (unknown subject) - Linux manual page....
XauFileName generates the default authorization file name by first checking the XAUTHORITY environment variable if set, else it returns $HOME/.Xauthority. This

We can't live, work or learn in freedom unless the software we use is free.