在一般的情况下,这里可以有多于一个的值列表(例如,list1 和 list2),并且每个值列表可以与一个循环变量的列表相关联。(例如,varlist1 和 varlist2)。 在循环的每次重复期间每个 varlist 中的变量被赋与相应的list 中的连续的值。在每个 list 中的值按从最先到最后的次序被使用,并且每个值被准确的使用一次。循环重复的总数足够的大来用光所有列表的所有的值。如果一个值的列表不包含足够元素,供给每次重复中的每个循环变量,则给遗漏的元素使用空值。
break 和 continue 语句可以在 body 中调用,与在 for 命令中有相同的效果。Foreach 返回一个空串。
set x {} foreach {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.
下一个循环使用 i 和 j 在两个并行的列表上重复。
set x {} foreach 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.
在下面例子中组合了两种形式。
set x {} foreach 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.
Copyright © 1993 The Regents of the University of California. Copyright © 1994-1996 Sun Microsystems, Inc. Copyright © 1995-1997 Roger E. Critchlow Jr.