This topic describes the syntax, parameters, return values, and examples of array functions.
arr_concat
Concatenates elements of an array into a single string, with an optional separator.
Syntax: arr_concat(tbl, sep) → String
| Parameter | Required | Default | Description |
|---|---|---|---|
tbl | Yes | — | The array whose elements you want to concatenate. |
sep | No | "" (empty string) | The separator inserted between elements. |
Example
d = ['t1','t2','t3']
say(arr_concat(d, '&'))
-- Output: t1&t2&t3arr_insert
Inserts an element into an array at a specified position.
Syntax: arr_insert(list, value, [pos]) → Boolean
| Parameter | Required | Default | Description |
|---|---|---|---|
list | Yes | — | The array to insert into. |
value | Yes | — | The element to insert. Accepts any data type. |
pos | No | End of array | The 1-based index at which to insert the element. Must be a non-zero integer. Elements after the insertion point shift toward the end. |
Returns true.
Example
tbl_1 = []
arr_insert(tbl_1, '1')
arr_insert(tbl_1, '3')
arr_insert(tbl_1, '5')
arr_insert(tbl_1, '2')
arr_insert(tbl_1, '6', 1)
str = arr_concat(tbl_1, '')
say(concat('arr_insert:', str))
-- Output: arr_insert:61352arr_remove
Removes an element from an array at a specified position and returns it.
Syntax: arr_remove(list, [pos]) → Any
| Parameter | Required | Default | Description |
|---|---|---|---|
list | Yes | — | The array to remove from. |
pos | No | Last element | The index of the element to remove. |
Returns the removed element.
Example
tbl_1 = []
arr_insert(tbl_1, '1')
arr_insert(tbl_1, '3')
arr_insert(tbl_1, '5')
arr_insert(tbl_1, '2')
say(concat('arr_remove:', arr_remove(tbl_1, 2)))
-- Output: arr_remove:3arr_sort
Sorts the elements of an array in place.
Syntax: arr_sort(list, [comp]) → Boolean
| Parameter | Required | Default | Description |
|---|---|---|---|
list | Yes | — | The array to sort. |
comp | No | ANSII ascending | A comparator function that determines element order. |
Comparator behavior
comp(a, b) return value | Sort order |
|---|---|
comp not provided | Elements are sorted by ANSII code in ascending order. Elements with equal ANSII values may be reordered. |
true | a is placed before b. |
false | b is placed before a. |
Note: Thecompfunction receives two elements fromlistas arguments. To sort in descending order, returntruewhena > b.
Returns true.
Example
tbl_1 = []
arr_insert(tbl_1, '1')
arr_insert(tbl_1, '3')
arr_insert(tbl_1, '5')
arr_insert(tbl_1, '2')
say(concat('remove:', arr_remove(tbl_1, 2)))
str = arr_concat(tbl_1, '')
say(concat('insert:', str))
arr_sort(tbl_1)
str = arr_concat(tbl_1, '')
say(concat('sort:', str))
def my_comp(a, b){
a = tonumber(a)
b = tonumber(b)
if gt(a, b) {
return true
}
return false
}
arr_sort(tbl_1, my_comp)
str = arr_concat(tbl_1, '')
say(concat('sort_comp:', str))
-- Output:
-- remove:3
-- insert:152
-- sort:125
-- sort_comp:521arr_len
Returns the number of elements in an array.
Syntax: arr_len(arr) → Number
| Parameter | Required | Description |
|---|---|---|
arr | Yes | The array to count. |
Example
d = []
set(d, 1, 'v1')
say(arr_len(d))
-- Output: 1