summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2004-11-26 10:02:16 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2004-11-26 10:02:16 (GMT)
commit53dbd43f21b5baf917383c6373da80babbbdc0f8 (patch)
tree677179c7c7dc176b9f8c9d1cdcb17e4c33cf9935 /doc
parent6ee1f25ba7a09bad8a410b44ff14dc80cfeb7bf1 (diff)
downloadtcl-53dbd43f21b5baf917383c6373da80babbbdc0f8.zip
tcl-53dbd43f21b5baf917383c6373da80babbbdc0f8.tar.gz
tcl-53dbd43f21b5baf917383c6373da80babbbdc0f8.tar.bz2
Added simple foreach example. [FRQ 1073334]
Diffstat (limited to 'doc')
-rw-r--r--doc/foreach.n25
1 files changed, 18 insertions, 7 deletions
diff --git a/doc/foreach.n b/doc/foreach.n
index 7054708..6c8d28e 100644
--- a/doc/foreach.n
+++ b/doc/foreach.n
@@ -5,7 +5,7 @@
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
-'\" RCS: @(#) $Id: foreach.n,v 1.4 2004/10/27 12:53:22 dkf Exp $
+'\" RCS: @(#) $Id: foreach.n,v 1.5 2004/11/26 10:02:23 dkf Exp $
'\"
.so man.macros
.TH foreach n "" Tcl "Tcl Built-In Commands"
@@ -51,37 +51,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 32
+set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun!
+puts "Value\\tSquare\\tCube" ;# Neat-looking header
+\fBforeach\fR x $values { ;# Now loop and print...
+ puts " $x\\t [expr {$x**2}]\\t [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)