Table of Contents

Search

  1. Preface
  2. Function reference
  3. Constants
  4. Operators
  5. Dates
  6. Functions
  7. System variables
  8. Datatype reference

Function Reference

Function Reference

Subscript operator (Arrays)

Subscript operator (Arrays)

Use a subscript operator to access elements in an array. You can access a specific element or a range of elements.

Syntax

To access a specific element in an array, use the following syntax:
array[ index ]
To access a range of elements in an array, use the following syntax:
array[ start_index , end_index ]
The following table describes the arguments in the syntax:
Argument
Description
array
Array data type. The array from which you want to access one or more elements.
You can enter any valid expression that evaluates to an array.
index
Integer data type. The position of the element that you want to access. For example, an index of 0 indicates the first element in an array.
start_index
Integer data type. The starting index in a range of elements that you want to access. The subscript operator includes the element that the starting index represents.
end_index
Integer data type. The ending index in a range of elements that you want to access. The subscript operator excludes the element that the ending index represents.
You can use an expression for the index that returns an integer value. If the expression returns a negative value, the index is considered to be 0.
If the specified index is greater than the size of the array minus 1, the index accesses the final element in the array.

Return Value

Element in the array. The return type is the same as the data type of the element.
If you specify two indices separated by a comma, such as
[i,j]
, the expression returns an array of the elements from
i
to
j-1
. If
i
is greater than
j
or the size of the array, the expression returns an empty array.
NULL in the following situations:
  • The index is greater than the size of the array.
  • The index is NULL.
  • You specify multiple indices such as
    [i,j]
    and either
    i
    or
    j
    is NULL.
  • The array is NULL.

Example

You have the following array of strings:
drinks = [‘milk’, ‘coffee’, ‘tea’, ‘chai’]
The following expressions use a subscript operator to access string elements in the array:
Input Value
RETURN VALUE
drinks[0]
'milk'
drinks[2]
'tea'
drinks[NULL]
NULL
drinks[1,3]
['coffee','tea']
drinks[2,NULL]
NULL
drinks[3,1]
[ ]

0 COMMENTS

We’d like to hear from you!