The statement ForEach … In is used to process elements in a comma-separated list.
Syntax
ForEach variable
In comma_separated_list
Do
... ;
Od;
ForEach variable
In list
Separator sep
Do
... ;
Od;
ForEach variable is the variable declared as Text. Common practice is to use the name Element for this variable to enhance
readability.In comma_separated_list is a list declared as a Text variable: Variable Text List = "1,2,3,..."Separator sep (optionally) is the text used to separate the list. If this is omitted or when sep is an empty textvi , the comma will be used as default separator.... ; This code to be repeated
The list is evaluated as a comma-separated list of values, where leading and trailing spaces are stripped.
Every element is assigned to the variable before the Do … Od code is executed.
Empty elements in the list are ignored.
The command Break can be used to terminate the loop unconditionally.
Note
An empty List will not trigger an error, the Do ... Od part of the statement will simply be skipped.
Example
Var Text Element;
Var Text List = "one, 2, yesterday";
ForEach Element
In List
Do
Progress Message (Element);
Od;
This script will send the progress messages "one", "2" and "yesterday" back to the client.
Note that in effect the variable Element is assigned the next value from List at each passing. This is why the messages in the above script are "one", "2" and "yesterday".
The keyword Separator allows the script to split the list on other strings:
Example
Var Text Element;
Var Text List = "one|+ 2|+ yesterday";
ForEach Element
In List
Separator "|+"
Do
Progress Message (Element);
Od;
The built-in variable _newline can be used to split multi-line output from external commands into the separate lines and process those lines separately.
Var Text Line;
RunCommand CmdLine (...);
ForEach Line
In _stdout
Separator _newline
Do
/* process the line */
...
Od;