Simple indexing is used to retrieve single elements from the set. Indexing begins with 0 which refers to the 1st
set element. The index value is specified in braces and shall not be confused with a set as such.
Cascaded indexing for retrieving elements from nested sets is also supported.
Following rules apply to the indexing sets:
echo( { a,b,c,d } {0} ); // Access the 1st element
a[] = { a,b,c, {d,e,f}, g }; // Assign a set to variable a[]
echo( "Last element : ", a[]{-1} ); // Retrieve the last element
echo( "Next element to the left: ", a[]{-2} ); // Retrieve the 2nd last element
echo( "Cascaded indexing : ", a[]{3}{2} ); // retrieve 3rd element, and inside the subset the 2nd element (cascaded indexing)
echo( "# elements in : ", a[]{} ); // Number of elements
echo( "# elements in nested set: ", a[]{-2}{} ); // Number of elements in subset (cascaded indexing)
echo( "Out of bounds case : ", a[]{99} ); // Empty is returned if index lies out of bounds
a
Last element : g
Next element to the left: {'d','e','f'}
Cascaded indexing : f
# elements in : 5
# elements in nested set: 3
Out of bounds case : {}
Simple indexing also works with write accesses in assignments. The target variable must contain an existing set. Values of other types are not allowed and will cause error messages. Any data type may be assigned. For example, a numeric element can be replaced by a string or set (a subset). Using multiple indexes as allowed for read accesses is not possible (e.g. [a]{1,2} = {A,B};).
a[] = { a,b,c, {d,e,f}, g }; // Assign a set to variable a[]
a[]{1} = B;
a[]{-1} = {G,H,I};
echo( "Modified value in a[] = ", a[] );
Modified value in a[] = {'a','B','c',{'d','e','f'},{'G','H','I'}}