foreach varlist1 list1 ?varlist2 list2 ...? body
foreach is a powerful control structure that allows looping over a list or multiple lists.
set alpha [list a b c d e f]
foreach {key} $alpha {
puts "key: $key"
}
Multiple variable names may be specified.
set alphaindexes [list a 1 b 2 c 3 d 4 e 5 f 6]
foreach {key num} $alphaindexes {
puts "key:$key num:$num"
}
Multiple lists can be iterated over at the same time.
set alpha [list a b c d e f]
set indexes [list 1 2 3 4 5 6]
foreach {key} $alpha {idx} $indexes {
puts "key: $key idx:$idx"
}