Documentation version 1.0 , 2009-12-19
Dataset of varying patronage: $ForEach{}
This powerful command allows to repeatedly evaluate a script
line. It consists of three parts, a variable which value is updated
with each evaluation of $ForEach{}, a range of values which defines how often the line
is evaluated and the values the variable
should contain, and a script line which will be re-evaluated with each loop. The script line may contain script commands itself,
and those are revaluated every time.
Example 1:
SCRIPT |
PREVIEW-RESULT |
$NewVar{count,integer} Some men play with colour-sided dice, showing either a fist or a sword in one of the colours. The first men throws his 3 dice: $ForEach{count,[1..3],<$OneOf{< black>,< red>,< green>}$OneOf{< fist>,< sword>}.>} |
Some men play with colour-sided dice, showing either a fist or a sword in one of the colours. The first men throws his 3 dice: red sword. green fist. red sword. |
Example 2:
SCRIPT |
PREVIEW-RESULT |
$NewVar{count,integer} The elf at this table tests samples of four different wines. $ForEach{count,[1..4],<Wine #$Var{count}: $DB{<%l>, <alcohol, wine>, 100, 1, 1} >} |
The elf at this table tests samples of four different wines. Wine #1: A sweet, rose wine. Wine #2: A medium, red, light wine made from Pear. Wine #3: A sweet, red, aromatized wine made from Cherry. Wine #4: A medium, red, light wine made from grapes. |
As you can see from the examples above, the range is defined
in the syntax [from..to] as with all variable arrays (see tutorial) and
the according value is stored in the counting variable. However, it is also
possible to use variables when defining the range:
Example 3:
SCRIPT |
PREVIEW-RESULT |
$NewVar{wines,integer,1,$Random{3,5}} $NewVar{count,integer} The elf at this table tests samples of $Var{wines} different wines. $ForEach{count,[1..wines],<Wine #$Var{count}: $DB{<%l>, <alcohol, wine>, 100, 1, 1} >} |
The elf at this table tests samples of 3 different wines. Wine #1: A dry, white, aromatized wine made from grapes. Wine #2: A medium, rose wine. Wine #3: A medium, white wine. |
The command can also be used silently to assign different values to an array.
Example 4:
SCRIPT |
PREVIEW-RESULT |
$NewVar{dice,string,10} $NewVar{count,integer} $ForEach{count,[1..$Dim{dice}],<$SetVar{dice[$Var{count}], $OneOf{<Fist>,<Sword>,<Shield>,<Eagle>,<Circle>,<Tree>}}>} 10 dice are thrown, they show: $Var{dice} |
10 dice are thrown, they show: Circle, Tree, Fist, Shield, Eagle, Tree, Fist, Circle, Shield, Eagle |
The $ForEach{} command is even more versatile! Instead of using a number
variable and a counting range, one can also use
variable arrays and let the command loop through the number of entries in the
array, always storing the according value in the
looping variable. The looping variable then has to be of the same type (integer
or string) as the array. (Note the difference to the
example above, where the index of an array was passed into the looping variable.)
Example 5:
SCRIPT |
PREVIEW-RESULT |
$NewVar{names,string,3,[<Marg>,<Sue>,<Liz>]} $NewVar{name,string} At this table sit three sisters: $Var{names} $ForEach{name,names,<$Var{name} wears a $OneOf{<red>,<blue>} $OneOf{<hat>,<scarf>}. >} |
At this table sit three sisters: Marg, Sue, Liz Marg wears a red scarf. Sue wears a blue scarf. Liz wears a red hat. |
Example 6:
SCRIPT |
PREVIEW-RESULT |
$NewVar{dice,integer,7,[4,6,8,10,12,20,100]} $NewVar{die,integer} A man throws a full set of multi-sided dice: $ForEach{die,dice,<D$Var{die}: $Random{1,$Var{die}} >} |
A man throws a full set of multi-sided dice: D4: 4 D6: 4 D8: 4 D10: 6 D12: 9 D20: 12 D100: 61 |
This second version of the $ForEach{} command is
especially useful in combination with array-resulting commands such as
$RPS{} command.