This version is out of date, covering development from v7.0.0 to v7.5.6. It is maintained here only for inbound reference links from elsewhere. It is no longer actively updated.

Jump to the current version of aTbRef

Tinderbox 7 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. 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:

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:

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.



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