sourCEntral - mobile manpages

pdf

foreach

______________________________________________________________________________

NAME

foreach − 在一個或多個列表的所有元素上重復

總覽 SYNOPSIS

foreach varname list body
foreach
varlist1 list1 ?varlist2 list2 ...? body _________________________________________________________________

描述 DESCRIPTION

foreach 命令實現一個循環,在這裏循環變量從一個或多個列表接受值。在最簡單的情況下,這裏有一個循環變量 varname ,和一個列表 list,它是要賦給 varname 的值的一個列表。body 參數是一個 Tcl 稿本。對於 list 的每個元素(按從最先到最後的次序),foreach 把這個元素的內容賦給 varname,如同使用 lindex 命令提取元素一樣,接著調用 Tcl 解釋器來執行 body

在一般的情況下,這裏可以有多於一個的值列表(例如,list1list2),並且每個值列表可以與一個循環變量的列表相關聯。(例如,varlist1varlist2)。 在循環的每次重復期間每個 varlist 中的變量被賦與相應的list 中的連續的值。在每 個list 中的值按從最先到最後的次序被使用,並且每個值被準確的使用一次。循環重復的總數足夠的大來用光所有列表的所有的值。如果一個值的列表不包含足夠元素,供給每次重復中的每個循環變量,則給遺漏的元素使用空值。

breakcontinue 語句可以在 body 中調用,與在 for 命令中有相同的效果。Foreach 返回一個空串。

範例 EXAMPLES

下面的循環使用 i 和 j 作為循環變量在一個單一的列表的一對元素上重復。 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.

參見 SEE ALSO

for(n), while(n), break(n), continue(n)

關鍵字 KEYWORDS

foreach, iteration, list, looping

[中文版維護人]

寒蟬退士

[中文版最新更新]

2001/08/05

《中國 Linux 論壇 man手冊頁翻譯計劃》:

http://cmpp.linuxforum.net

pdf