Adding inter-item join data to a loop output
The simplest way, if the end output is a string, is to make a list from the loop and use List.format("formatStr") to make a string from this list inserting formatStr between each list item (and not before /after the ends).
However, as explored below, it may be desirable to add the item-joining string whilst iterating the loop.
Adding join data: add at loop start or finish of an item?
As a general point, and perhaps counter-intuitively, it is better to test at start "am I second or later?" and prepend the join, rather than test "am I last?" and append the join. this is because it is easier to know definitely that a process has started as opposed to if/when it will end.
Examples of loop Detection of loop start or end are shown under detecting first or last item context in loops.
Adding join data in-loop
An example is making a list of { } enclosed JSON data objects where each {object}
has a comma character as a join but there must not be a trailing comma after the last item. Thus a comma must be added either:
- before every item, for all except the first item
- after every item for all except the last item
Which approach is used is a matter of personal choice.