88. Determine the Last Repetition of a REPEAT
Description
How to determine when a REPEAT has reached its last repetition.
Code
""
REPEAT RepeatedDialog
IF COUNTER = COUNT( RepeatedDialog )
// Do something here
END IF
END REPEAT
Example:
""
REPEAT Consultants
// First pass
IF RESULT = ""
FullName-t
// Last pass
ELSE IF COUNTER = COUNT( Consultants )
RESULT + ", and last but not least, "
RESULT + FullName-t
// Somewhere in the middle
ELSE
RESULT + ", " + FullName-t
END IF
END REPEAT
Explanation
The standard way to check whether you're on the last repetition of a REPEAT is to look if the COUNTER is equal to the COUNT of the dialog. By way of explanation, COUNTER tells what repetition you are on. On the first pass, COUNTER = 1; on the second pass, COUNTER = 2, and so on. COUNT holds the total number of REPEAT levels. So when COUNTER = COUNT, you know that you have reached the last level and hence the last repetition.
The example is of a repeated dialog, "Consultants", that collects the names of consultants. This dialog has just one text variable, FullName-t. We begin by setting the computation to an empty string, "". We then begin our REPEAT. On the first pass, the RESULT will still be an empty string, "", so we will simply retrieve the first consultant's name. On the subsequent passes while COUNTER < COUNT we add a comma, a space, and the next name. But when COUNTER = COUNT, we know we've reached the last repetition. So this time we'll first add on the string ", and last but not least, " and then the final name. The output will look like this:
Jon Jones, Kate King, Lem Larson, Matt Miller, and last but not least, Norm Nelson