Looping through occurrences without a counter

Tony Marston - 7th March 2001

This is a typical example of the code that is used to loop through all the occurrences in a hitlist. Note that it uses a numeric variable to keep track of the current position within the hitlist.

$counter$ = 1
setocc "entity",1
while ($status >= 0)
    ....
    $counter$ = $counter$+1
    setocc "entity",$counter$
endwhile

Although this code works there is the potential for problems, such as:

This is the technique that I prefer to use when looping through occurrences. The function $curocc(entity) always identifies the current occurrence for the named entity, and this can be used in a mathematical expression to provide the number of the next (or previous) occurrence.

setocc "entity",1
while ($status >= 0)
    ....
    setocc "entity",$curocc(entity)+1
endwhile

By not using a separate variable for a counter it eliminates the potential for mistakes. It is also (slightly) more efficient.

If you have a situation where an occurrence can be dropped from the hitlist once it has been processed then replace the setocc with discard. This will drop the current occurrence and make the next one active. Note that after the last occurrence has been processed what you are left with is an empty non-database occurrence, hence the use of $dbocc in the condition, as in the following code sample:

setocc "entity",1
while ($dbocc(entity))
    ....
    discard "entity",$curocc(entity)
endwhile

Tony Marston
7th March 2001

mailto:tony@tonymarston.net
mailto:TonyMarston@hotmail.com
http://www.tonymarston.net

counter