repeat over, #item, #last, #first

control structure

Syntax
repeat over shelf-name (as alias)? (& shelf-name (as alias)?)*
   local-declaration*
   action*
again


Purpose

You can use repeat over to process each item on a shelf in turn. The following program creates a shelf with five items and outputs each item in turn:

  process
     local integer digits initial {1,2,3,4,5}
  
     repeat over digits & letters
        output "d" % digits || "%n"
     again

Within the body of the repeat over loop, the current item on the shelf is advanced one position for each iteration of the loop. This means that you can refer to the current item by the name of the shelf alone, without an indexer, as in the example above. You can still reference other items on the shelf using indexers.

You can repeat over more that one shelf at a time, providing that all the shelves are the same size:

  process
     local integer digits initial {1,2,3,4,5}
     local stream letters initial {"A", "B", "C", "D", "E"}
  
     repeat over digits & letters
        output letters
            || "d" % digits
            || "%n"
     again

Within a repeat over loop, three built in variable are available to help you determine which iteration is being executed. #first and #last are switch variables that return true when the loop is in the first and last iterations respectively:

  process
     local integer digits initial {1,2,3,4,5}
     local stream letters initial {"A", "B", "C", "D", "E"}
  
     repeat over digits & letters
        output "[" when #first
        output letters
            || "d" % digits
        output ", " unless #last
        output  "]%n" when #last
     again

#item returns an integer corresponding to the number of iterations of the loop, starting at 1 (one):

  process
     local stream letters initial {"A", "B", "C", "D", "E"}
  
     repeat over letters
        output "[" when #first
        output letters
            || "d" % #item
        output ", " unless #last
        output  "]%n" when #last
     again

You can specify an alias for the shelf that you are repeating over. The alias name is specified using the as keyword following the name of the shelf. Specifying an alias is required when you iterate over a shelf that is a field of a record:

  declare record foo
   field stream letters initial {"A", "B", "C", "D", "E"}
  
  process
     local foo x
     repeat over x:letters as letters
        output "[" when #first
        output letters
            || "d" % #item
        output ", " unless #last
        output  "]%n" when #last
     again

Notes

A using statement inside a repeat over loop will reset the current item to that specified in the using but will not affect the sequence or number of iterations.

You can add an item to a shelf inside a repeat over loop, but only at a position greater that the size of the shelf when the loop began. The number of iterations in the loop is determined before the first iteration, and adding more items to the shelf from within the loop will not change the number of iterations being performed. You can remove items from a shelf within a repeat over loop, but only items that were added within the same loop.

repeat over can also be used to process the following:

  • successive tokens from a list-valued attribute: repeat over attribute and repeat over data-attribute
  • successive attributes of a currently open element or data entity: repeat over attributes and repeat over data-attributes
  • the current element stack: repeat over current elements
  • the referents shelf: repeat over referents
  • compiled DTDs: repeat over xml-dtds and repeat over sgml-dtds

Related Syntax
Related Concepts