Tinderbox v9 Icon

Constructing $Attribute references in loops

Tinderbox's loop mechanism using .each() is helpful but one task can be problematic. This where the task is to loop a list of attribute title values and then act upon each attribute in turn.

The basic problem is that if the loop variable is, for example 'X' and current loop value is 'MyString', how to construct the reference $MyString, so as to act on the MyString attribute.

The answer is to use a nested action() call inside the loop, even if that seems counter-intuitive at first sight. For example, consider the task of resetting every attribute in a note's displayed attributes table. The contents of the table are stored in the Set-type system attribute $DisplayedAttributes, which can be iterated with .each():


Now the task is to set X, in each loop as a reference to the attribute named in the current value using action():


In other words, if X is value 'MyString' the action() is both concatenating some strings:


and then evaluating the resulting expression:


Note that:

It may be necessary to nest quotes. Consider a list of String-type values. To set each one to a value of 'test':


Note that the target value needs to be enclosed in quotes. As the main expression string already uses double quotes, the string needs to use single quotes. It does not matter if you reverse the nesting. The action would work just as well in the form:


But, do ensure the sets of single/double quotes nest correctly.

The action can use loop variable more than once. Take the last scenario, but this time where all the attributes are Set or List type. Here the code is:


…i.e. creating an action expression, in-loop, like this for X value 'MyList':


White space in the expression does not matter, as in normal action expressions, but is omitted above for brevity/clarity. The expression could also be:

$MyList = $MyList + 'test'; 

…and still work.

A Tinderbox Reference File : Actions & Rules : Constructing $Attribute references in loops