index operator

Syntax

  • float array[float index]
  • string array[float index]
  • bool array[float index]

Parameters

  1. array—(float [], string[], bool[])
    Array for which an item is requested.
  2. index
    Zero-based index of array item.

Returns

The item of array at position index. If index is negative or greater or equal than the size of array a default value is returned which is 0, false or "" respectively.

Description

The item function is called as an operator on an array using a bracket notation []. It allows for a random access of elements at a given zero-based index.

Related

Examples

Print items

const array = comp(fe) { all : comp.index + 1 }

Example --> print(array)       // (4)[1,2,3,4]
            print(size(array)) // 4
            print(array[0])    // 1
            print(array[3])    // 4
            print(array[-1])   // 0
            print(array[4])    // 0

Recursive item selection

largestIndex(array) = largestIndex(array, 0, 0)

largestIndex(array, i, iLargest) =
    case i == size(array) : iLargest
    else :
        case array[i] > array[iLargest] : largestIndex(array, i+1, i)
        else                            : largestIndex(array, i+1, iLargest)

const edgeLengths = comp(e) { all : scope.sx }

longestEdge = largestIndex(edgeLengths)
                                        
Lot --> comp(e) { longestEdge : LongestEdge. }

With the size function and item operator an array can recursively be parsed for a specific item value. In this example the index of the longest edge is retrieved from an array of edge lengths.


In this topic