summaryrefslogtreecommitdiffstats
path: root/doc/foreach.n
diff options
context:
space:
mode:
Diffstat (limited to 'doc/foreach.n')
-rw-r--r--doc/foreach.n29
1 files changed, 20 insertions, 9 deletions
diff --git a/doc/foreach.n b/doc/foreach.n
index be7a6fb..654c0cf 100644
--- a/doc/foreach.n
+++ b/doc/foreach.n
@@ -35,9 +35,9 @@ In the general case there can be more than one value list
and each value list can be associated with a list of loop variables
(e.g., \fIvarlist1\fR and \fIvarlist2\fR).
During each iteration of the loop
-the variables of each \fIvarlist\fP are assigned
-consecutive values from the corresponding \fIlist\fP.
-Values in each \fIlist\fP are used in order from first to last,
+the variables of each \fIvarlist\fR are assigned
+consecutive values from the corresponding \fIlist\fR.
+Values in each \fIlist\fR 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.
@@ -49,37 +49,48 @@ The \fBbreak\fR and \fBcontinue\fR statements may be
invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
command. \fBForeach\fR returns an empty string.
.SH EXAMPLES
+This loop prints every value in a list together with the square and
+cube of the value:
+.CS
+'\" Maintainers: notice the tab hacking below!
+.ta 3i
+set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun!
+puts "Value\etSquare\etCube" ;# Neat-looking header
+\fBforeach\fR x $values { ;# Now loop and print...
+ puts " $x\et [expr {$x**2}]\et [expr {$x**3}]"
+}
+.CE
.PP
The following loop uses i and j as loop variables to iterate over
pairs of elements of a single list.
-.DS
+.CS
set x {}
\fBforeach\fR {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.
-.DE
+.CE
.PP
The next loop uses i and j to iterate over two lists in parallel.
-.DS
+.CS
set x {}
\fBforeach\fR 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.
-.DE
+.CE
.PP
The two forms are combined in the following example.
-.DS
+.CS
set x {}
\fBforeach\fR 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.
-.DE
+.CE
.SH "SEE ALSO"
for(n), while(n), break(n), continue(n)