Array Subscript operator [ ]

This section describes how you reference multi-occurs fields and expressions in formula processing.

Single Occurrence

You can reference a single occurrence of a multi-occurs expression using the [ ] array subscript operator like this:
multi-occurs expression[subscript expression]

multi-occurs expression is typically a field reference, but it can be any expression.
subscript expression should be an expression that evaluates to an integer between 1 and the number of occurrences in the first expression. If the number is out of range either a compiler time or a run time error will be generated.

Except for character fields, there is nothing unusual about the [] operator compared with other languages. For character fields the [] operator is very unusual indeed. For a character field, if the multi-occurs expression is a field reference, and subscript expression is an integer constant, and the [ is immediately next to the last character of the field reference, with no intervening spaces, then the [] operator behaves as you might expect - it extracts the complete occurrence from the field. In any other situation the [] operator extracts a single character from the field - if subscript expression evaluates to n then the result is the nth character in multi-occurs expression.

Examples

Suppose our program defines the following fields with the given values:

Field Type Value
ADDRESS  C30*4  123 Cherry Lane
Stokehampton
Cheshire
ST8 5LN
BUDGET  N5*12 1
2
3
4
5
6
7
8
9
10
11
12
NAME  C30 James Parrot Seed
I I5 1

The table below shows the result of using the [] operator in various ways:

Expression Result
address[1]
budget[1]
name[1]
address[2]
'123 Cherry Lane'
1
'James Parrot Seed'
'Stokehampton'
address[i]
budget[i]
name[i]
address[2][i]
'1'
1
'J'
'S'
address [1]
budget [1]
name [1]
address [2]
'1'
1
'J'
'2'
address[1][10]
name[1][10]
'y'
'r'
(address)[10]
(name)[10]
'y'
'r'

The syntax address [1] to extract the first character of the address, is probably sufficiently confusing that you should avoid using it. Using the double subscript [1][1], or the parenthesised form is probably better.

To refer to a range of characters within a character expression you must use the head() or tail() functions or both in combination.

Range of Occurrences

You can reference a range of occurrences using the subscript operator and two full stops, [ .. ]. The range must be specified by constant values, and multi-occurs expression must just be a field reference. If you want a range defined by variables, you must use a combination of head()and tail(). Using the example fields from above we have:

Expression Result
BUDGET[2..4] 2
3
4
ADDRESS[2..3] 'Stokehampton
Cheshire'