For Each Statement

Use a generic iterator for string tokenization.

foreach is a generic iterator for string tokenization, array/list member access and stream manipulation. Braces are mandatory. break and continue may be used – break to terminate the loop (as previously described) and continue to restart the loop from the beginning with the next value.

There are two syntaxes for foreach – both operate in the same way, use whichever you prefer:

foreach(name: expression) {
 // loop-body

foreach name in expression {
 // loop-body


Here is an example of a String iterator – this splits the specified string using the global variable IFS as a separator. If IFS is not set, the default is " \t" (space or tab). The iterator can also remove quotes if the global variable DEQUOTE is true. e.g.

set IFS ='|';
set DEQUOTE = true;
foreach(bar:'alpha|"beta"|gamma|delta') {
echo $bar; // alpha, beta, gamma, delta.

Here is an example of an _Array iterator_ – this iterates over all the keys in the specified array (i.e.: the subscripts). e.g.

set arr = {'a' =\>'1','b' =\>'2','c' =\>'3' };
foreach(k: $arr) {

// $k will iterate through the subscripts "a", "b" and "c"
echo "$k =\> ${arr[$k]}"; // output will be a =\> 1 etc


Here is an example of a Stream iterator – this splits a stream on newlines (and/or carriage returns) e.g.

echo"Hello\nWorld" \>\> $stream1;
foreach(line: $stream1) {
echo $line;

// output is

// Hello

// World

This can be used to set _DMScript_ variables from a file. Assume we have a file in a repository called "myfile.txt" containing the following:

# example file containing variables to set


var2="hello there"

var3="this is a test"


You can set these variables in _DMScript_ by using the following code:

using stream $varfile; // create the stream

using _DropZone_ "tdz" {

checkout(repository: "Openmake\_Demo.demorep2",pattern: "myfile.txt");

read(file: "myfile.txt", stream: $varfile);

foreach(line: $varfile) {

set line=${line.regsub('#.\*$',"")}; // remove comments

if (${line.length()}\>0 & ${line.instr("=")}\>0) {

eval("set ${line};");




echo "var1=$var1";

echo "var2=$var2";

echo "var3=$var3";

echo "var4=$var4";



var2=hello there

var3=this is a test

Last modified May 19, 2020: after dmscript (aa9d9ae)