1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1993 The Regents of the University of California.
|
3 |
|
|
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
4 |
|
|
'\"
|
5 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
6 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
7 |
|
|
'\"
|
8 |
|
|
'\" RCS: @(#) $Id: foreach.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH foreach n "" Tcl "Tcl Built-In Commands"
|
12 |
|
|
.BS
|
13 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
14 |
|
|
.SH NAME
|
15 |
|
|
foreach \- Iterate over all elements in one or more lists
|
16 |
|
|
.SH SYNOPSIS
|
17 |
|
|
\fBforeach \fIvarname list body\fR
|
18 |
|
|
.br
|
19 |
|
|
\fBforeach \fIvarlist1 list1\fR ?\fIvarlist2 list2 ...\fR? \fIbody\fR
|
20 |
|
|
.BE
|
21 |
|
|
|
22 |
|
|
.SH DESCRIPTION
|
23 |
|
|
.PP
|
24 |
|
|
The \fBforeach\fR command implements a loop where the loop
|
25 |
|
|
variable(s) take on values from one or more lists.
|
26 |
|
|
In the simplest case there is one loop variable, \fIvarname\fR,
|
27 |
|
|
and one list, \fIlist\fR, that is a list of values to assign to \fIvarname\fR.
|
28 |
|
|
The \fIbody\fR argument is a Tcl script.
|
29 |
|
|
For each element of \fIlist\fR (in order
|
30 |
|
|
from first to last), \fBforeach\fR assigns the contents of the
|
31 |
|
|
element to \fIvarname\fR as if the \fBlindex\fR command had been used
|
32 |
|
|
to extract the element, then calls the Tcl interpreter to execute
|
33 |
|
|
\fIbody\fR.
|
34 |
|
|
.PP
|
35 |
|
|
In the general case there can be more than one value list
|
36 |
|
|
(e.g., \fIlist1\fR and \fIlist2\fR),
|
37 |
|
|
and each value list can be associated with a list of loop variables
|
38 |
|
|
(e.g., \fIvarlist1\fR and \fIvarlist2\fR).
|
39 |
|
|
During each iteration of the loop
|
40 |
|
|
the variables of each \fIvarlist\fP are assigned
|
41 |
|
|
consecutive values from the corresponding \fIlist\fP.
|
42 |
|
|
Values in each \fIlist\fP are used in order from first to last,
|
43 |
|
|
and each value is used exactly once.
|
44 |
|
|
The total number of loop iterations is large enough to use
|
45 |
|
|
up all the values from all the value lists.
|
46 |
|
|
If a value list does not contain enough
|
47 |
|
|
elements for each of its loop variables in each iteration,
|
48 |
|
|
empty values are used for the missing elements.
|
49 |
|
|
.PP
|
50 |
|
|
The \fBbreak\fR and \fBcontinue\fR statements may be
|
51 |
|
|
invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
|
52 |
|
|
command. \fBForeach\fR returns an empty string.
|
53 |
|
|
.SH EXAMPLES
|
54 |
|
|
.PP
|
55 |
|
|
The following loop uses i and j as loop variables to iterate over
|
56 |
|
|
pairs of elements of a single list.
|
57 |
|
|
.DS
|
58 |
|
|
set x {}
|
59 |
|
|
foreach {i j} {a b c d e f} {
|
60 |
|
|
lappend x $j $i
|
61 |
|
|
}
|
62 |
|
|
# The value of x is "b a d c f e"
|
63 |
|
|
# There are 3 iterations of the loop.
|
64 |
|
|
.DE
|
65 |
|
|
.PP
|
66 |
|
|
The next loop uses i and j to iterate over two lists in parallel.
|
67 |
|
|
.DS
|
68 |
|
|
set x {}
|
69 |
|
|
foreach i {a b c} j {d e f g} {
|
70 |
|
|
lappend x $i $j
|
71 |
|
|
}
|
72 |
|
|
# The value of x is "a d b e c f {} g"
|
73 |
|
|
# There are 4 iterations of the loop.
|
74 |
|
|
.DE
|
75 |
|
|
.PP
|
76 |
|
|
The two forms are combined in the following example.
|
77 |
|
|
.DS
|
78 |
|
|
set x {}
|
79 |
|
|
foreach i {a b c} {j k} {d e f g} {
|
80 |
|
|
lappend x $i $j $k
|
81 |
|
|
}
|
82 |
|
|
# The value of x is "a d e b f g c {} {}"
|
83 |
|
|
# There are 3 iterations of the loop.
|
84 |
|
|
.DE
|
85 |
|
|
.SH KEYWORDS
|
86 |
|
|
foreach, iteration, list, looping
|