Product SiteDocumentation Site

A.3. Repetitive Loops over Collections

A collection loop specifies a control variable, control2, which receives a different value on each repetition of the loop. (For more information on control2, see Section 2.4, “DO”.) These different values are taken from successive values of collection. The collection is any expression that evaluates to an object that provides a MAKEARRAY method, including stem variables. The collection returned determines the set of values and their order. Array, List, and Queue items return an array with the items in the appropriate order, as do Streams. Tables, Stems, Directories, etc. are not ordered so the items get placed in the array in no particular order.
If the collection is a stem variable, the values are the tail names that have been explicitly assigned to the given stem. The order of the tail names is unspecified, and a program should not rely on any order.
For other collection objects, the MAKEARRAY method of the specific collection class determines the values assigned to the control variable.
All values for the loop iteration are obtained at the beginning of the loop. Therefore, changes to the target collection object do not affect the loop iteration. For example, using DROP to change the set of tails associated with a stem or using a new value as a tail does not change the number of loop iterations or the values over which the loop iterates.
As with controlled repetition, you can specify the symbol that describes the control variable on the END clause. The control variable is referenced by name, and you can change it within the loop (although this would not usually be useful). You can also specify the control variable name on an ITERATE or LEAVE instruction.

Example A.7. Simple LOOP block over a collection

Astem.=0
Astem.3="CCC"
Astem.24="XXX"
loop k over Astem.
   say k Astem.k
end k

This example can produce:
3 CCC
24 XXX
or:
24 XXX
3 CCC
See Figure A.1, “Concept of a Loop” for a diagram.