Returns the array[index] if it is valid or else returns the default value.
index( array, index, [default] )
array: (Any Type Array) A homogeneous array, whose indexed value is expected.
index: (Integer or Integer Array) The index or array of indices of the array.
default: (Any Type) The default value to be returned if the array or the index is invalid, such as if the array itself is empty or the index is < 1 or > array length.
Any Type
The type of the default value must be same as that of the elements in the array.
You can experiment with this function in the test box below.
Test Input
Retrieving a value at a single index:
index({10, 20, 30}, 2, 1)
returns 20
Return the value of a field name within a CDT where the array is the PV and the index is the field name:
index(pv!person, "firstName", "")
returns the value of the "firstName" field in a CDT of person with two fields (firstName
and lastName
)
Retrieving values at multiple indices:
index({10, 20, 30}, {1, 3}, 0)
returns 10, 30
Retrieving values at repeated indices:
index({10, 20, 30}, {1, 1, 3}, 0)
returns 10, 10, 30
Retrieving the value of a CDT field using the field name as the index argument:
index(topaginginfo(1, 10), "startIndex", 0)
returns 1
property(): This function acts as an alias to the index() function especially when applied over custom data types.