Use complex operators to access elements in an array, map, or struct. You can use complex operators only in advanced mode.
The following table lists the complex operators:
Operator
Meaning
[ ]
Subscript operator.
Use a subscript operator with an array or a map:
- In an array, use a subscript operator to access one or more elements.
- In a map, use a subscript operator to access the value corresponding to a given key in a key-value pair.
.
Dot operator.
Use a dot operator with a struct or an array of structs:
- In a struct, use a dot operator to access an element.
- In an array of structs, use a dot operator to access elements in each struct. The operator returns the elements of the same name within each struct as an array.
To access elements in a nested hierarchy, you can use a combination of complex operators.
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]
[ ]
Subscript operator (Maps)
Use a subscript operator to access the value corresponding to a given key in a key-value pair.
Syntax
To access the value corresponding to a given key in a map, use the following syntax:
map[ key ]
The following table describes the arguments in the syntax:
Argument
Description
map
Map data type. The map from which you want to retrieve the value corresponding to a key.
key
Data type of the key. The key element for which you want to retrieve the value.
You can enter any valid expression that evaluates to a key value of the map data.
Return Value
The value associated with the key in the map. The return type is the same as the data type of the value.
The following expressions use a dot operator to access the elements in each struct in the array:
Input Value
RETURN VALUE
employee_info_array.name
['Derrick','Kevin','Lauren']
employee_info_array.city
[NULL,'Redwood City','Woodcliff Lake']
employee_info_array.state
['NY','CA',NULL]
Complex operators for nested hierarchies
A nested hierarchy contains elements that also contain hierarchical data, such as an array of structs. Use a combination of complex operators to access elements in a nested hierarchy.
You can access elements in the following types of nested hierarchies:
•Multidimensional array
•Array of structs
•Struct with array elements
•Nested struct
Multidimensional array
A multidimensional array is an array of arrays. You can use a subscript operator to access a primitive element in an array at the innermost level. You can also use a subscript operator to access an array at any level.
You can use subscript operators to return the following values:
•A primitive element in an array at the innermost level.
•One or more arrays at any level.
•A subset of one or more arrays at any level.
To access a primitive element in an array at the innermost level, you use more than one subscript operator. The number of dimensions in a multidimensional array determines the number of subscript operators to use. Each subscript operator must contain one index value. The data type of the return value is the same as the data type of the primitive elements in the array.
For example, in a two-dimensional array, you use two subscript operators. The first subscript operator accesses the parent array. The second subscript operator accesses the child array within the parent array.
Examples
Consider the following two-dimensional parent array that contains three child arrays and each child array contains string elements:
An array of structs is an array with struct elements. Use a combination of subscript and dot operators to access a child struct and the elements in the child struct.
To access an element in a child struct within a parent array, use a subscript operator followed by a dot operator. You can also reverse the order of the operators without changing the return value.
Examples
You have the following array of structs employee_info_array:
You can access an element in one of the child structs using complex operators in either of the following orders:
You use a subscript operator and then a dot operator.
The operators access the array of structs in the following order:
1The subscript operator accesses the indexed element in the array and returns a struct.
2The dot operator accesses an element in the struct.
For example, the following expressions use a subscript operator followed by a dot operator to access elements in the array employee_info_array:
Input Value
RETURN VALUE
employee_info_array[0].name
'Derrick'
employee_info_array[1].city
'Redwood City'
employee_info_array[2].state
NULL
You use a dot operator and then a subscript operator.
The operators access the array of structs in the following order:
1The dot operator locates elements with the same name from each of the structs and returns an array.
2The subscript operator accesses the indexed element in the array.
For example, the following expressions show the return value when you use a dot operator to access elements in the array employee_info_array:
Input Value
RETURN VALUE
employee_info_array.name
['Derrick','Kevin','Lauren']
employee_info_array.city
[NULL,'Redwood City','Woodcliff Lake']
employee_info_array.state
['NY','CA',NULL]
The following expressions show the return value when you use a dot operator followed by a subscript operator to access elements in the array employee_info_array:
Input Value
RETURN VALUE
employee_info_array.name[0]
'Derrick'
employee_info_array.city[1]
'Redwood City'
employee_info_array.state[2]
NULL
Note that the return values are the same whether you use a subscript operator or a dot operator first. For example, the expressions employee_info_array[0].name and employee_info_array.name[0] have the same return value 'Derrick'.
Struct with array elements
To access elements in an array within a struct, use a dot operator followed by a subscript operator. The dot operator first accesses the specified array in the struct. Then, the subscript operator accesses an element in the array based on the index value.
Example
You have the following struct with arrays drinks, sandwiches, and salads:
If you use the expression menu_struct.drinks[0], the operators access the parent struct and child arrays in the following order:
1The dot operator accesses the array drinks.
2The subscript operator accesses the value at position 0 in the array drinks: ['milk','coffee','tea','chai'] and returns 'milk'.
The following expressions show more examples that use a dot operator followed by a subscript operator to access values in the child arrays in the parent struct menu_struct:
Input Value
RETURN VALUE
menu_struct.drinks[1]
'coffee'
menu_struct.sandwiches[2]
NULL
menu_struct.salads[3]
'chipotle'
menu_struct.drinks[0,3]
['milk','coffee','tea']
Nested struct
A nested struct is a struct that contains one or more levels of structs. You can use a dot operator to access a primitive element in a struct at the innermost level. You can also use a dot operator to access a struct at any level.
You can use dot operators to return the following values:
•A primitive element in a struct at the innermost level.
•One or more structs at any level.
To access a primitive element in a struct at the innermost level, you use more than one dot operator. The number of levels in a nested struct determines the number of dot operators to use. The data type of the return value is the same as the data type of the element in the struct.
For example, in a nested struct with two levels of structs, you use two dot operators. The first dot operator accesses the parent struct to locate the child struct. Then, the second dot operator accesses the child struct to return a specific primitive element in the child struct.
Example
You have the following struct employee_info_struct that contains two child structs home_address_info and department_info:
employee_info_struct{
emp_name: 'Derrick'
home_address_info{ city: 'New York' state: NULL
department_info{ NULL }
}
The following expressions use dot operators to access values from the struct employee_info_struct: