Contact TLCC

Access members of a list easily Click here to see all the Developer/Admin Tips

Date tip published:11/17/2003
Description:Prior to Release 6, nested @Subset functions were used to access an element in a list. Release 6 includes a list subscript operator which makes it easy to access an element in a list.


To learn more about designing Notes and Domino 6 applications use the following links:

Notes Domino 6 Application Development Update
Notes Domino 6 Application Development 2
Notes Domino 6 Certified Developer Package



List Subscript Operator

Prior to Release 6, nested @Subset functions were used to access an element in a list. Release 6 includes a list subscript operator which makes it easy to access an element in a list.

Syntax
List Subscript Operator

The syntax for the list subscript operator is:


    list[elementNumber]

    list is any variable or field that contains a list of values.

    elementNumber is a numeric value which indicates the number position of the element to return from the list.




Technique: Old versus New

Prior to Release 6, nested @Subset functions were used to access an element in a list. Here is the code to access the third element in the Names list using nested @Subset functions:
@Subset(@Subset(Names; 3); -1)

And this code example shows how the new list subscript operator can be used to easily access the third element in a list.
Names[3]


Note
Cannot use List Subscript Operator to Assign a Value to a List


The list subscript operator can be used to access an element in a list, but not to assign a value to a list. For example, the following will not work:
Names[3] := "Hello"


Note
First Element Differs for List Subscript Operator and LotusScript Arrays


The first element in a formula list is subscript one [1]. This differs from LotusScript arrays, where the first element is subscript zero (0).


Lotus Domino Designer 6 Help


The complete syntax for List Subscript Operator is copied here from Lotus Domino Designer 6 Help.
List Subscript Operator
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)

Note that the Categories field containing the list must be located above or to the left of the field containing this code or the formula returns an "Array index out of bounds" error.

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 also 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])

Note that the subscript operator that follows the concatenation operator (:) must be surrounded by parentheses.