OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tcl8.6.12 / doc / foreach.n
1 '\"
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 .TH foreach n "" Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note:  do not modify the .SH NAME line immediately below!
12 .SH NAME
13 foreach \- Iterate over all elements in one or more lists
14 .SH SYNOPSIS
15 \fBforeach \fIvarname list body\fR
16 .br
17 \fBforeach \fIvarlist1 list1\fR ?\fIvarlist2 list2 ...\fR? \fIbody\fR
18 .BE
19
20 .SH DESCRIPTION
21 .PP
22 The \fBforeach\fR command implements a loop where the loop
23 variable(s) take on values from one or more lists.
24 In the simplest case there is one loop variable, \fIvarname\fR,
25 and one list, \fIlist\fR, that is a list of values to assign to \fIvarname\fR.
26 The \fIbody\fR argument is a Tcl script.
27 For each element of \fIlist\fR (in order
28 from first to last), \fBforeach\fR assigns the contents of the
29 element to \fIvarname\fR as if the \fBlindex\fR command had been used
30 to extract the element, then calls the Tcl interpreter to execute
31 \fIbody\fR.
32 .PP
33 In the general case there can be more than one value list
34 (e.g., \fIlist1\fR and \fIlist2\fR),
35 and each value list can be associated with a list of loop variables
36 (e.g., \fIvarlist1\fR and \fIvarlist2\fR).
37 During each iteration of the loop
38 the variables of each \fIvarlist\fR are assigned
39 consecutive values from the corresponding \fIlist\fR.
40 Values in each \fIlist\fR are used in order from first to last,
41 and each value is used exactly once.
42 The total number of loop iterations is large enough to use
43 up all the values from all the value lists.
44 If a value list does not contain enough
45 elements for each of its loop variables in each iteration,
46 empty values are used for the missing elements.
47 .PP
48 The \fBbreak\fR and \fBcontinue\fR statements may be
49 invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
50 command.  \fBForeach\fR returns an empty string.
51 .SH EXAMPLES
52 .PP
53 This loop prints every value in a list together with the square and
54 cube of the value:
55 .PP
56 .CS
57 '\" Maintainers: notice the tab hacking below!
58 .ta 3i
59 set values {1 3 5 7 2 4 6 8}    ;# Odd numbers first, for fun!
60 puts "Value\etSquare\etCube"    ;# Neat-looking header
61 \fBforeach\fR x $values {       ;# Now loop and print...
62     puts " $x\et [expr {$x**2}]\et [expr {$x**3}]"
63 }
64 .CE
65 .PP
66 The following loop uses i and j as loop variables to iterate over
67 pairs of elements of a single list.
68 .PP
69 .CS
70 set x {}
71 \fBforeach\fR {i j} {a b c d e f} {
72     lappend x $j $i
73 }
74 # The value of x is "b a d c f e"
75 # There are 3 iterations of the loop.
76 .CE
77 .PP
78 The next loop uses i and j to iterate over two lists in parallel.
79 .PP
80 .CS
81 set x {}
82 \fBforeach\fR i {a b c} j {d e f g} {
83     lappend x $i $j
84 }
85 # The value of x is "a d b e c f {} g"
86 # There are 4 iterations of the loop.
87 .CE
88 .PP
89 The two forms are combined in the following example.
90 .PP
91 .CS
92 set x {}
93 \fBforeach\fR i {a b c} {j k} {d e f g} {
94     lappend x $i $j $k
95 }
96 # The value of x is "a d e b f g c {} {}"
97 # There are 3 iterations of the loop.
98 .CE
99
100 .SH "SEE ALSO"
101 for(n), while(n), break(n), continue(n)
102
103 .SH KEYWORDS
104 foreach, iteration, list, loop