foreach.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:3k
- '"
- '" Copyright (c) 1993 The Regents of the University of California.
- '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
- '"
- '" 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.3.18.1 2004/10/27 12:52:40 dkf Exp $
- '"
- .so man.macros
- .TH foreach n "" Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- foreach - Iterate over all elements in one or more lists
- .SH SYNOPSIS
- fBforeach fIvarname list bodyfR
- .br
- fBforeach fIvarlist1 list1fR ?fIvarlist2 list2 ...fR? fIbodyfR
- .BE
- .SH DESCRIPTION
- .PP
- The fBforeachfR 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, fIvarnamefR,
- and one list, fIlistfR, that is a list of values to assign to fIvarnamefR.
- The fIbodyfR argument is a Tcl script.
- For each element of fIlistfR (in order
- from first to last), fBforeachfR assigns the contents of the
- element to fIvarnamefR as if the fBlindexfR command had been used
- to extract the element, then calls the Tcl interpreter to execute
- fIbodyfR.
- .PP
- In the general case there can be more than one value list
- (e.g., fIlist1fR and fIlist2fR),
- and each value list can be associated with a list of loop variables
- (e.g., fIvarlist1fR and fIvarlist2fR).
- During each iteration of the loop
- the variables of each fIvarlistfP are assigned
- consecutive values from the corresponding fIlistfP.
- Values in each fIlistfP 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.
- .PP
- The fBbreakfR and fBcontinuefR statements may be
- invoked inside fIbodyfR, with the same effect as in the fBforfR
- command. fBForeachfR returns an empty string.
- .SH EXAMPLES
- .PP
- The following loop uses i and j as loop variables to iterate over
- pairs of elements of a single list.
- .DS
- set x {}
- fBforeachfR {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
- .PP
- The next loop uses i and j to iterate over two lists in parallel.
- .DS
- set x {}
- fBforeachfR 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
- .PP
- The two forms are combined in the following example.
- .DS
- set x {}
- fBforeachfR 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
- .SH "SEE ALSO"
- for(n), while(n), break(n), continue(n)
- .SH KEYWORDS
- foreach, iteration, list, looping