foreach(type name) list {commands}
Loop through a block of commands once for each member of a list.
type name
list
commands
foreach(String s) {"Hello", "World"} { print $s, " " } print "\n"Note that the following script does the same thing.
listof String sl = {"Hello", "World"} foreach(String s) $sl { print $s, " " } print "\n"
foreach
command repeats a sequence of
commands for different values of given parameter. For example,
when scheduling a telescope, the list that you give the foreach
command could be a list of sources that you want to observe. On
the first iteration of the loop, the loop variable is set to the
first member of the specified list. On the second iteration, it
is set to the second member of the list, and so on until the end
of the list is reached. The foreach statement then
terminates. You can leave the loop before the last element of the
list has been reached by using the break
command. Similarly,
the next iteration of the loop can be started before
the current iteration has been completed via the
next
command.
Note that assignments to the loop variable also change the value of the list variable that it represents. This allows lists to be rewritten.