SNIPPET - FOREACH
The FOREACH command allows a block of commands to be repeated once for each element in an array. The syntax is as follows:
FOREACH <var> in <array>
{
<commands to repeat>
[ BREAK ]
}
The <var> is the name of a variable which will reference each element in the array – also called an iterator variable. You can treat it just as any other script variable however, it is only temporary. Once the array loop finishes, that name will no longer exist. If it is a name that was in use prior to entering the loop, the original value will be lost.
The <array> is the name of a variable which contains an array. You can create an array using the pipe symbol to delimit elements as in “one|two|three|…”. You can also explicitly assign array elements one at a time: ASSIGN x[10] = “ten”. No pre-initialization is required. If elements below the index being assigned have not been assigned, they will be automatically initialized blank.
When inside the loop, the iterator variable can be altered. If it is changed, the original array will reflect the changes:
ASSIGN i=1
REPEAT 10
{
ASSIGN num[i]="{i}"
ASSIGN i=i+1
}
FOREACH item in num
{
SWITCH item
{
CASE 4 { ASSIGN item="FOUR" }
CASE 5 { ASSIGN item="FIVE" }
}
}
Output:
NUM=1|2|3|FOUR|FIVE|6|7|8|9|10
The key word BREAK may be used within the block of commands to force early termination of the loop.
Attention: The FOREACH command has a built-in time limit of ½ second. This is to protect against abusive looping which costs platform performance. A loop that exceeds this threshold will be aborted.



