Sometimes when working with list items using .each() you need to do text (or code text) formatting like inserting a comma between items. Looping makes this difficult as you don’t want the same ‘fill’ applied to every items, but rather all except the last.
If you have a List or set and simply want to reformat it to string using a delimiter such as comma-and-a-space, then List.format() does the job. But…
If you need to act on the source list or perhaps construct new per-item values, then .format()
can’t help. So, what to do? Here list.first and list.last are you friends.
In the demo below, I will not do anything complex with the list items but I will turn the list into a textual list, placed into $Text, but with:
- add text before the first item
- a comma+space after each list item except the last
- add text after the last item.
As shown here:
Here I use a stamp but the same logic can be used elsewhere. This is the code:
$MyList.each(anItem){
if(anItem==$MyList.first){
$Text+="Starting at list item 1: ";
};
$Text+=anItem;
if(anItem!=$MyList.last){
$Text+=", ";
}else{
$Text+=". Done!";
};
};
I could have split the last two tasks into two discrete if()
tests, but as the ‘filler’ is added to all except the last list item, the else
branch of the text must match only the last item. So, we can make the test more compact.
So, if you were wondering how to test for the first or last item in a list, you know know.
Here is the TBX document used to make the demo (minor changes from grab above, e.g. stamp name): test-list-position.tbx (77.2 KB)
Select the note ‘xx’ and apply the ‘Process list’ stamp to it. A stamp is also supplied to clear existing $Text.
I’ve made a note to self for aTbRef to better cross-reference documentation of .each()
with .first
and .last
,