Cursor is a list variable which has an internal index which is controlled by a small number of operators. The index value may be set, incremented, decremented or changed by adding an integer value. These are the cursor index operators:
| Operator | Description |
|---|---|
++ | Postfix operator increments the index |
-- | Postfix operator decrements the index |
+ | Resets index to 0, the default value |
- | Resets index to position of last item in list |
= | Sets index to value on right hand side |
+= | Adds to index value on right hand side |
A cursor can be dereferenced the same as any list variable using the [] operator, but the value in the expression
is interpreted as relative to the internal index value. Thus a value of 0 references the item at the current
cursor position. This means negative values can be used.
Format
A cursor can only be declared in a template or calculator and only as an attachment. The cursor may optionally be assigned a type if type conversion is desired:
cursor[<type>] name(list-name);
A cursor is ready for use upon creation, with the initial internal index value set to zero. To commence navigation from a different position,
the index may be set by any of these operators: [ = - += ]. To return to the initial state, use + operator.
++ and -- for calculator and extends to + and - for template.
Pet Names Example
The PetNames application of tutorial9 uses a cursor named "pet_cursor" to iterate through a list of pet details in XML format, extract the name of each pet and export the names as a list. The companion ReversePetNames application creates the same list in reverse. This is the calculator for the first application:
+ export list<string> pet_names;
+ cursor pet(pets_info);
(
"^.*" + nameRegex +".*",
{
regex (pet++) == petRegex { name },
pet_names += name
Note how the fact attribute of the cursor is used to detect the end of the list has been reached. It is good practice to check the cursor fact status before reading the cursor in case the list is empty for some unexpected reason. Note too that the cursor expression "pet++" is in parentheses because regex syntax needs to distinguish between expressions and identifiers this way. An infinite loop would occur if the regex failed to match without the cursor having been advanced first.
