The List/Set.each() function can be used to iterate lists. It works like a 'for each' method commonly used in programming and scripting whereby each and every item in the source list is processed using the in-loop code.
Using a list item more than once per loop
Assume your data in a list or set. In this case $MySet contains the values cow/dog/eel. Now assume it is necessary to turn that into an HTML select list like this:
<select>
<option value="cow">cow</option>
<option value="dog">dog</option>
<option value="eel">eel</option>
</select>
Notice how in the output above each source list item is used more than once. However, using format() or List.format() you can only wrap each list item once. So other than doing iterative list formatting - which can get complex - .each() offers a way around this. Use this $Rule - line breaks and indentation are just for clarity:
$MyList =;
$MySet.each(X) {
$MyString = '<option value="'+X+'">'+X+'</option>';
$MyList = $MyList + $MyString;
};
$Text = "<select>\n"+$MyList.format("\n")+"\n</select>";
Shorter version, without the in-loop caching of the concatenated per-item string (see bullet #3 below for the rationale):
$MyList =;
$MySet.each(X) {
$MyList = $MyList + ("<option>"+X+"</option>");
};
$Text = "<select>\n"+$MyList.format("\n")+"\n</select>";
Notes:
- $MyList must be reset (to an empty list) before the loop otherwise each time the Rule fire the loop adds to existing $MyList data rather than build a new set.
- Everything between the { } is runs as the loop. Here the closing } has a semi-colon after it as another discrete section of code follows it.
- Ordinarily, one could simply put parentheses around the string literals to concatenate them before passing a single string into $MyList. This works as long as the string literals to concatenate don't contain single or double quotes, or, if double quoted enclosed strings contain single quotes. But, an edge case occurs where single-quote-enclosed strings contain any double-quotes; in such cases an 'interim' string attribute is needed to ensure the sub-strings are correctly concatenated before the data is passed to $MyList.
- As a string attribute only contains one value - adding a new one replaces the old. Thus $MyString does not need to be reset before the loop unlike a set or list such as $MyList.
- Note that for the final concatenation .format() can be used as each list item is used only once in the resulting output. The closing select tag must be preceded with a '\n' line break as the .format()'s join only adds the concatenation string between each item in $MyList and a lone break is needed after the last option close tag.
Adding a loop counter
By default .each() processed every list item. But what if you only won't to do something with the first, last or Nth item or perhaps only even numbered ones? In that case you will need to create your own loop counter, and iterate in-loop. This sums the value of odd-numbered items of $MyList onto the existing value of $MyNumber:
$MyCountNum = 0;
$MyList.each(ListItem) {
if(mod($MyCountNum,2)==0) {
$MyNumber = MyNumber + ListItem;
};
$MyCountNum = $MyCountNum + 1;
}
Notes:
- Set the counter ($MyCountNum) to zero (or just reset the attribute) before starting the loop.
- Tinderbox list values number upwards from zero (e.g. if accessing data via the List.at() function). If you like to think of list item #1 being 1, then set $MyCountNum to 1 before the loop and test for
mod($MyCountNum,2)!=0
. - Note that list.at() is read-only so you cannot use code like
$MyList.at($MyCountNum) = ListItem;
Using a path variable
The loop variable can be a path and this be used as a variable designator for attribute offset references inside the loop:
$Text=""; collect($Overdue,$Path).each(x){
$Text = $Text+":"+$Text(x);
}
In the above, the 'x' variable is a $Path value and is being used to provide the offset reference in the loop.
Using variable within a loop
A variable created by var() can be altered from within an .each() loop.