This version is out of date, covering development from v9.5.0 to v9.7.3. 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 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 is in a List or Set type attribute. In this case $MyList contains the values cow/dog/eel. 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 the following $Rule, line breaks and indentation are just for clarity:

	$MyList =;
	$MyList.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 =;
	$MyList.each(X) {
		$MyList = $MyList + ("<option>"+X+"</option>");
	};
	$Text = "<select>\n"+$MyList.format("\n")+"\n</select>";

Notes:

Detecting first or last loop items

This is discussed separately in detecting first or last item context in loops. Also see: List/Set.first and List/Set.last.

Adding output joins in-loop

See adding joins in loops.

Adding a loop counter

By default .each() processes every list item. But what if you only want do something with the Nth item or perhaps only even numbered ones? In that case it is necessary to create a loop counter, and iterate in-loop. For instance, the following 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:

An alternate method is to use the range operator (see example).

Using a path variable

The loop variable can itself be a path and this be used as a variable designator allowing attribute offset addressing 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 address argument in the loop.

Using a variable within a loop

A variable created by var can be altered from within an .each() loop, as shown here.

Creating an $AttributeReference within a loop

Described here.