Array operators

You can use these array operators to manage data stored in arrays.

You must initialize an array using either ar_new or ar_by_len. The other two most common array operations are ar_get (to retrieve a value from the array) and ar_set (to put a value in the array).

Operator

Function

Example

Result

ar_new(...)

Initializes a new array from a list of elements.

[arr = ar_new(1, 2, 3, 4)]

arr is the array {1, 2, 3, 4}

ar_by_len(length, value)

Initializes a new array of size length, with all elements initialized to value.

[arr = ar_by_len(3, "abc")]

arr is the array {"abc", "abc", "abc"}

ar_get(array, index)

Retrieves the element at position index in array. Array indexes are 0-based.

[arr = ar_new("a", "b", "c")] [ar_get(arr, 2)]

Returns "c"

ar_set(array, index, value)

Sets the element at position index in array equal to value. Array indexes are 0-based.

[arr = ar_new(1, 2, 3)] [ar_set(arr, 1, 4)]

arr is the array {1, 4, 3}

ar_incr(array, index, delta)

Adds delta to the element at position index in array. Array indexes are 0-based.

[arr = ar_new(1, 2, 3)] [ar_incr(arr, 0, -3)]

arr is the array {-2, 2, 3}

ar_append(array, ...)

Appends a list of elements to the end of array.

[arr = ar_new(1, 2, 3)] [ar_append(arr, 4, 5)]

arr is the array {1, 2, 3, 4, 5}

ar_concat(array1, array2)

Creates a new array containing all of the elements of array1 followed by all the elements of array2.

[arr1 = ar_new("a", "b", "c")] [arr2 = ar_new("d", "e", "f")] [arr3 = ar_concat(arr1, arr2)]

arr3 is the array {"a", "b", "c", "d", "e", "f"}. arr1 and arr2 are unchanged.

ar_copy(array)

Creates a new array containing all of the elements of array1.

[arr1 = ar_new(1, 2, 3)] [arr2 = ar_copy(arr1)]

arr2 is the array {1, 2, 3}. arr1 is unchanged.

ar_len(array)

Returns the number of elements in array.

[arr = ar_new("a", "b", "c", "d")] [ar_len(arr)]

Returns 4