Tinderbox v9 Icon

Using .each() for loops

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. Assume it is necessary to turn that into an HTML select list like this:

<option value="cow">cow</option>
<option value="dog">dog</option>
<option value="eel">eel</option>

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 the following $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>";


Adding a loop counter

By default .each() processed every list item. But what if you only will not 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;


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.

Creating an $AttributeReference within a loop

Described here.

A Tinderbox Reference File : Actions & Rules : Using .each() for loops