List subscript operator

The list subscript operator ([]) returns one element of a list.

Note: The list subscript operator is new with Release 6.

This example returns element 2 of the Categories field:

Categories[2]

A subscript consists of a numeric value in brackets. The numeric value can be a constant, variable, or expression. Decimals are rounded to integers. A subscript follows the list name.

The following example uses a variable subscript to iterate through a list.

n := 1;
@While(n <= @Elements(Categories);
   @Prompt([OK]; "Category " + @Text(n); Categories[n]);
   n := n + 1
)

The first element of a list is subscript [1]. A subscript that is less than [1] or that is greater than the number of elements in the list returns the "Array index out of bounds" error.

The subscript operator is valid for any data type that allows lists (text, number, and date/time) even if the data entity is scalar. The subscript operator is only valid for data types that do not allow lists (richtext) when a subscript of [1] is used; this returns its current value unchanged.

The subscript operator cannot be used on the left side of an assignment statement. That is, you cannot assign a value to a subscripted element. You must build the complete list and then assign it. For example, if Categories is a 3-element list and you want to assign a new value to element 2:

FIELD Categories := Categories[1] : "CatNew" : Categories[3]