Index

foreach(type name) list {commands}

Loop through a block of commands once for each member of a list.

Arguments:
type name
The datatype and name to give the loop variable. The datatype must match that of members of the following list.
list
The list to iterate through. The loop variable will be set to the value of the next member of this list on each iteration of the loop.
commands
The list of commands to be executed by the loop.

Example:
The following example prints the contents of a list of strings, as a single line of words separated by spaces.
 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"
Context:
The 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.

Martin Shepherd (9-Oct-1997)