index operator
array float[]string[]bool[] Array for which one or more elements are requested.
index, rowIndex, colIndex float Zero-based linear, row, or column index of an array element.
indices, rowIndices, colIndices float[]bool[] Arrays containing zero-based indices, or logical values indicating whether a specific index is included.
Returns an array element or a new array.
Indexing with index values
The index operator returns the array element at a specific zero-based index position, or at a specific row and column in a 2D array.
array = [1,2,3,4]
array[0] 1
array[3] 4
array2d = [1,2,3;
4,5,6;
7,8,9]
array2d[1,1] 5
Indexing with index arrays
Array elements can also be indexed by an array. In that case, the index operator returns a new array whose dimensions are prescribed by the dimensions of the index array.
array[[0,2]] [1,3] array[[1,1]] [2,2] array[3:-1:1] [4,3,2] array[[0,1;2,3]] [1,2; 3,4] array2d[1,[0,2]] [4,6] array2d[[0,0,1],2] [3; 3; 6] array2d[0:2,[0,2]] [1,3; 4,6; 7,9]
Logical indexing
An index array can also be given by logical values indicating whether a specific index is included.
array[[true,false,true]] [1,3] array[array .> 2] [3,4] array[array .> 1 .&& array .<= 3] [2,3] array2d[[false,true,true],0] [4; 7]
Invalid indexing
If an index is negative or greater than or equal to the number of elements, rows, or columns respectively, a default value is returned. The default value is 0 for float arrays, false for bool arrays, and "" for string arrays.
array[-1] 0 array[[false,false,true,true,true]] [3,4,0] array2d[[2,3],-1:1] [0,7,8; 0,0,0]
Linear indexing
If a 2D array is indexed in 1D, linear indexing is used. Indexing is performed row-wise starting with the first row and continuing on successive rows.
array2d[3] 4 array2d[0:size(array2d)-1] [0,1,2,3,4,5,6,7,8,9] array2d[[0;3;6]] [1; 4; 7] array2d[0,[0,1;2,3]] [1,2,3,0] array[[true,false;true,false]] [1,3]
Related
Examples
Recursive element selection
indexOfLargest(array) = indexOfLargest(array, 0, 0)
indexOfLargest(array, i, iLargest) =
case i == size(array) : iLargest
else :
case array[i] > array[iLargest] : indexOfLargest(array, i+1, i)
else : indexOfLargest(array, i+1, iLargest)
const edgeLengths = comp(e) { all : scope.sx }
indexOfLongestEdge = indexOfLargest(edgeLengths)
Lot --> comp(e) { indexOfLongestEdge : LongestEdge. }
With the size function and the index operator, an array can be recursively parsed for a specific element value. In this example the index of the longest edge is retrieved from an array of edge lengths.
Parsing text file
// table
// a;b;c
// d;e;f
// g;h;i
const file = readTextFile("table.txt")
const cells = splitString(file, "$;|\n") // [a,b,c,d,e,f,g,h,i]
const columns = 3
const indexes = [0 : columns : size(cells)-1] // [0:3:6]
const firstCol = cells[indexes] // [a,d,g]
A text file containing a table is parsed and elements are indexed linearly.
Read CSV table
const table = readStringTable("table.csv")
const firstCol = table[0 : nRows(table)-1, 0] // [a,d,g]
A CSV file is read and elements are indexed using the 2D index operator.
Indexing by 2D arrays
const a = ["_", "d", "i", "a", "g"]
const b = [1,0,0,0;
0,2,0,0;
0,0,3,0;
0,0,0,4]
const c = a[b]
// (4x4)
// d _ _ _
// _ i _ _
// _ _ a _
// _ _ _ g
The result of the 1D index operator is an array that has the same dimensions as the indices array.
Indexing out of bounds
zeros(elems) = floatArray[1:elems] ones(rows,cols) = floatArray[1:rows,1:cols] .+ 1 const a = zeros(4) // [0,0,0,0] const b = ones(2,3) // [1,1,1 ; 1,1,1]
An empty floatArray is indexed by colon sequences. This creates a new array filled with the default value, which is zero for float arrays.
Prime numbers
findPrimes(max) = case max <= 1 : floatArray else : findPrimes(sqrt(max), [2 : max], floatArray) findPrimes(limit, a, primes) = case a[0] <= limit : findPrimes(limit, a[a .% a[0] .!= 0], [primes, a[0]]) else : [primes, a] Lot --> print(findPrimes(20)) // (8)[2,3,5,7,11,13,17,19]
An implementation of the Sieve of Eratosthenes that uses indexing by boolean arrays.