ForEach...In...Do...Od

You can use the statement ForEach … In 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.

Example

Var Text List="1,2,3"; Variable Text List = "1,2,3,..."

Separator sep (optional) is the text used to separate elements in the list. If omitted or when sep is an empty text, the comma is used as default separator.

... ; is the code to be repeated for each element in the list.

The list is evaluated as a 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.

Example

Var Text List = "1,2,4"
The third empty element is ignored. Only for the elements 1, 2, and 4 the code between Do ... Od is executed.

You can use the command Break to terminate the loop unconditionally.

If the List is empty, an error is not triggered, and the code between the Do ... Od part of the statement is not executed.

Example

Var Text Element;
Var Text List = "one, 2, yesterday";

ForEach Element
In List
Do
  Progress Message (Element);
Od;

This script sends 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. As a result, the messages in the preceding 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;

You can use the built-in variable _newline to split multiline 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;