Language version: 2 · Reference version: 2.7 · Date: 2026-10-25 · Compiler build: df7b3b14 · Namespace: array · Entries: 67
array (library)
Definition
Functions for creating and working with arrays: ordered, resizable lists of values of one type. They cover creation (array.new with optional initial_value, the typed array.new_* constructors and array.from), reading and editing elements, searching and sorting, and statistics over the elements such as array.avg, array.stdev and array.percentile_nearest_rank.
Arrays declared with static keep their elements from bar to bar, which makes them the usual tool for rolling windows and for keeping a limited history of drawings.
Notes
- An
array.<name>(a, ...)function can also be called on the array asa.<name>(...):a.push(x),a.get(i),a.set(i, x),a.length(),a.sum(),a.first(),a.last(),a.pop()anda.clear()all compile. There is noa.size(): the name islength. - The numeric functions (
abs,avg,covariance,max,median,min,mode,percentile_linear_interpolation,percentile_nearest_rank,percentrank,range,standardize,stdev,sumandvariance) take an array of int or float elements. Another element type fails the check, for examplearray.sumon a string array: “Error inarray.sum(): array type must be int or float”. sort,sort_indices,joinand the threebinary_searchforms take an array of int, float or string elements;everyandsometake bool, int or float. Another element type fails the check, for example “Error inarray.sort(): array type must be int, float or string”.- The numeric functions that return one value leave out
naelements and returnnawhen none is left (an empty array, or onlynaelements);stdev,varianceandcovariancealso returnnawhenbiasedis false and fewer than two values are left.absandstandardizereturn an array instead: each slot is kept, anaelement staysna, and an empty array gives an empty array.array.covariancestops the script with a runtime error when its two arrays differ in size.
array.abs (function)
Definition
Returns a new array holding the absolute value of each element. The original array is not changed.
Syntax
Returns
series array
Example
array.avg (function)
Definition
Returns the mean of the array’s elements.
Syntax
Returns
series float
Example
array.binary_search (function)
Definition
Searches an array sorted in ascending order for value and returns its index, or -1 when it is not there. The array must already be sorted.
Syntax
Returns
series int
Example
array.binary_search_leftmost (function)
Definition
Searches an array sorted in ascending order. When value is found, returns the index of its first occurrence; otherwise returns the index at which value would be inserted to keep the array sorted: the index of the first element larger than value, or the length of the array when every element is smaller. The array must already be sorted.
Syntax
Returns
series int
Example
array.binary_search_rightmost (function)
Definition
Searches an array sorted in ascending order. When value is found, returns the index of its last occurrence; otherwise returns the index at which value would be inserted to keep the array sorted: the index of the smallest element larger than value, or the length of the array when every element is smaller. The array must already be sorted.
Syntax
Returns
series int
Example
array.clear (function)
Definition
Removes every element, leaving an empty array. The array itself remains usable.
Syntax
Returns
void
Example
array.concat (function)
Definition
Appends the elements of id2 to the end of id1 and returns id1. id2 is not changed.
Syntax
Returns
series array
Example
array.copy (function)
Definition
Returns a new array with the same elements.
The copy has its own slots and its own length: replacing, adding or removing an element of the copy does not change the original. It is a shallow copy: elements that are objects of a custom type or drawings are the same ones in both arrays, so a change made inside one of them shows through both.
Syntax
Returns
series array
Example
array.covariance (function)
Definition
Returns the covariance of two arrays of the same size.
biased chooses the estimate: true (the default) divides by the number of elements, false by one less.
Syntax
Returns
series float
Example
array.every (function)
Definition
Returns true when every element is true. It takes a bool, int or float array: a number counts as true when it is not zero. An na element makes the result false, and for an empty array the result is true.
Syntax
Returns
series bool
Example
array.fill (function)
Definition
Sets elements of the array to value. With no index arguments every element is set; index_from and index_to limit the change to a range, where index_to is not included.
Syntax
Returns
void
Example
array.first (function)
Definition
Returns the first element of the array. The array must not be empty.
Syntax
Returns
series <type of the array's elements>
Example
array.from (function, overload 1)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. Int and float values can be mixed (array.from(1, 2.5) is an array<float>); a string cannot be mixed with another type. At most 4000 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<int>
Example
array.from (function, overload 2)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. Int and float values can be mixed (array.from(1, 2.5) is an array<float>); a string cannot be mixed with another type. At most 4000 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<float>
Example
array.from (function, overload 3)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 4000 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<bool>
Example
array.from (function, overload 4)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<string>
Example
array.from (function, overload 5)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 4000 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<color>
Example
array.from (function, overload 6)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<label>
Example
array.from (function, overload 7)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<line>
Example
array.from (function, overload 8)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<box>
Example
array.from (function, overload 9)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<linefill>
Example
array.from (function, overload 10)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<polyline>
Example
array.from (function, overload 11)
Definition
Creates an array from the values passed to it, in order.
The element type is the type of the overload. At most 999 values are accepted; more stop the script with a runtime error.
Syntax
Returns
series array<table>
Example
array.get (function)
Definition
Returns the element at index. Indexes start at 0; an index outside the array is an error.
Syntax
Returns
series <type of the array's elements>
Example
array.includes (function)
Definition
Returns true when value is one of the array’s elements.
Syntax
Returns
series bool
Example
array.indexof (function)
Definition
Returns the index of the first element equal to value, or -1 when no element matches.
Syntax
Returns
series int
Example
array.insert (function)
Definition
Inserts value at index, moving the element that was there, and every element after it, up one index.
Syntax
Returns
void
Example
array.join (function)
Definition
Joins the elements into one string, with separator between them.
Syntax
Returns
series string
Example
array.last (function)
Definition
Returns the last element of the array. The array must not be empty.
Syntax
Returns
series <type of the array's elements>
Example
array.lastindexof (function)
Definition
Returns the index of the last element equal to value, or -1 when no element matches.
Syntax
Returns
series int
Example
array.length (function)
Definition
Returns the number of elements in the array.
Syntax
Returns
series int
Example
array.max (function)
Definition
Returns the largest element. With nth, returns the nth largest instead, counting from 0.
The result has the element type of the array: int for an int array, float for a float array. na elements are left out. The result is na when none is left, and with nth when nth is negative or not smaller than the number of elements left.
Syntax
Returns
series <type of the array's elements>
Example
array.median (function)
Definition
Returns the median of the array’s elements: the middle value once they are sorted, or the mean of the two middle values when the count is even.
Syntax
Returns
series float
Example
array.min (function)
Definition
Returns the smallest element. With nth, returns the nth smallest instead, counting from 0.
The result has the element type of the array: int for an int array, float for a float array. na elements are left out. The result is na when none is left, and with nth when nth is negative or not smaller than the number of elements left.
Syntax
Returns
series <type of the array's elements>
Example
array.mode (function)
Definition
Returns the most frequent element. When several values are equally frequent, the smallest of them is returned.
The result has the element type of the array: int for an int array, float for a float array.
Syntax
Returns
series <type of the array's elements>
Example
array.new (function)
Definition
Creates an array of the given length. The element type is written in angle brackets, as in array.new<float>(10).
Optional initial_value fills every slot in one step (elements stay na when it is omitted). The value must match the array’s element type.
Syntax
Returns
series array
Notes
array.new<bool>(n)starts withnaelements, whilearray.new_bool(n)starts withfalse.
array.new_bool (function)
Definition
Creates an array of bool elements with size elements, each set to initial_value.
Both arguments are optional: leave size out for an empty array, and initial_value out for elements that start as false. This differs from the other typed constructors and from array.new<bool>, whose elements start as na.
Syntax
Returns
series array<bool>
Example
array.new_box (function)
Definition
Creates an array of box IDs with size elements, each set to initial_value.
Arrays of drawing IDs are the usual way to keep a limited history of drawings: push each new box and delete the oldest when the array grows past a limit. Both arguments are optional; elements start as na when initial_value is left out.
Syntax
Returns
series array<box>
Example
array.new_color (function)
Definition
Creates an array of color elements with size elements, each set to initial_value.
Both arguments are optional: leave size out for an empty array, and initial_value out for elements that start as na.
Syntax
Returns
series array<color>
Example
array.new_float (function)
Definition
Creates an array of float elements with size elements, each set to initial_value.
Both arguments are optional: leave size out for an empty array, and initial_value out for elements that start as na.
Syntax
Returns
series array<float>
Example
array.new_int (function)
Definition
Creates an array of int elements with size elements, each set to initial_value.
Both arguments are optional: leave size out for an empty array, and initial_value out for elements that start as na.
Syntax
Returns
series array<int>
Example
array.new_label (function)
Definition
Creates an array of label IDs with size elements, each set to initial_value.
Arrays of drawing IDs are the usual way to keep a limited history of drawings: push each new label and delete the oldest when the array grows past a limit. Both arguments are optional; elements start as na when initial_value is left out.
Syntax
Returns
series array<label>
Example
array.new_line (function)
Definition
Creates an array of line IDs with size elements, each set to initial_value.
Arrays of drawing IDs are the usual way to keep a limited history of drawings: push each new line and delete the oldest when the array grows past a limit. Both arguments are optional; elements start as na when initial_value is left out.
Syntax
Returns
series array<line>
Example
array.new_linefill (function)
Definition
Creates an array of linefill IDs with size elements, each set to initial_value.
Arrays of drawing IDs are the usual way to keep a limited history of drawings: push each new linefill and delete the oldest when the array grows past a limit. Both arguments are optional; elements start as na when initial_value is left out.
Syntax
Returns
series array<linefill>
Example
array.new_string (function)
Definition
Creates an array of string elements with size elements, each set to initial_value.
Both arguments are optional: leave size out for an empty array, and initial_value out for elements that start as na.
Syntax
Returns
series array<string>
Example
array.new_table (function)
Definition
Creates an array of table IDs with size elements, each set to initial_value.
Arrays of drawing IDs are the usual way to keep a limited history of drawings: push each new table and delete the oldest when the array grows past a limit. Both arguments are optional; elements start as na when initial_value is left out.
Syntax
Returns
series array<table>
Example
array.percentile_linear_interpolation (function)
Definition
Returns the value below which percentage percent of the elements fall, interpolating between the two nearest elements.
Syntax
Returns
series float
Example
array.percentile_nearest_rank (function)
Definition
Returns the value below which percentage percent of the elements fall, using the nearest element rather than interpolating.
The result has the element type of the array: int for an int array, float for a float array. A percentage below 0 or above 100 is taken as 0 or 100.
Syntax
Returns
series <type of the array's elements>
Example
array.percentrank (function)
Definition
Returns the percentile rank of the element at index: the percentage of elements that are less than or equal to it.
Syntax
Returns
series float
Example
array.pop (function)
Definition
Removes the last element of the array and returns it.
Syntax
Returns
series <type of the array's elements>
Example
array.push (function)
Definition
Appends value to the end of the array, growing it by one element.
Syntax
Returns
void
Example
array.range (function)
Definition
Returns the difference between the largest and the smallest element.
The result has the element type of the array: int for an int array, float for a float array.
Syntax
Returns
series <type of the array's elements>
Example
array.remove (function)
Definition
Removes the element at index. It returns nothing. The elements after it move down one index.
To keep the element, read it with array.get before the call.
Syntax
Returns
void
Example
array.removeAll (function)
Definition
Removes all elements from the array, leaving it empty.
Syntax
Returns
void
Example
array.reverse (function)
Definition
Reverses the order of the elements, in place.
Syntax
Returns
void
Example
array.set (function)
Definition
Replaces the element at index with value. The array keeps its size.
Syntax
Returns
void
Example
array.shift (function)
Definition
Removes the first element of the array and returns it. The remaining elements move down one index.
Paired with array.push, it keeps a fixed-length window of recent values.
Syntax
Returns
series <type of the array's elements>
Example
array.slice (function)
Definition
Returns the part of the array from index_from up to, but not including, index_to.
The slice has its own length and shares the original’s element slots over that range: replacing an element in one (with array.set, for example) shows in the other. The two lengths are independent, but an operation that shifts or overwrites shared slots (removing an element from the slice, for example) can change the original’s contents too, and adding elements may move one of them to separate storage. Use array.copy on the slice when you need an independent array.
Syntax
Returns
series array
Example
array.some (function)
Definition
Returns true when at least one element is true. It takes a bool, int or float array: a number counts as true when it is not zero. na elements are skipped, and for an empty array the result is false.
Syntax
Returns
series bool
Example
array.sort (function)
Definition
Sorts the array in place. order is order.ascending (the default) or order.descending.
Syntax
Returns
void
Example
array.sort_indices (function)
Definition
Returns an int array of indexes that would sort the array, leaving the array itself unchanged. order is order.ascending (the default) or order.descending.
Syntax
Returns
series array<int>
Example
array.standardize (function)
Definition
Returns a new array of z-scores: each element minus the mean, divided by the standard deviation. The original array is not changed.
Syntax
Returns
series array<float>
Example
array.stdev (function)
Definition
Returns the standard deviation of the array’s elements.
biased chooses the estimate: true (the default) divides by the number of elements, false by one less.
Syntax
Returns
series float
Example
array.sum (function)
Definition
Returns the sum of the array’s elements.
The result has the element type of the array: int for an int array, float for a float array.
Syntax
Returns
series <type of the array's elements>
Example
array.unshift (function)
Definition
Inserts value at the beginning of the array. The existing elements move up one index.
Syntax
Returns
void
Example
array.variance (function)
Definition
Returns the variance of the array’s elements.
biased chooses the estimate: true (the default) divides by the number of elements, false by one less.
Syntax
Returns
series float
Example