Array functions let you build, modify, and inspect arrays in EdgeScript. Use them together to implement custom routing logic in Application Load Balancer (ALB).
arr_concat
Joins all elements of an array into a single string, with an optional separator between each element.
Syntax
arr_concat(tbl: Array, sep: String) → StringParameters
| Parameter | Required | Description |
|---|---|---|
tbl | Yes | The array whose elements to concatenate. |
sep | No | The separator string inserted between each element. Defaults to an empty string. |
Return value
A string of all elements joined by sep. If sep is omitted, elements are joined with no separator.
Example
d = ['t1', 't2', 't3']
say(arr_concat(d, '&'))
-- Output: t1&t2&t3arr_insert
Inserts an element into an array at a specified position. See also arr_remove, which removes elements from an array.
Syntax
arr_insert(list: Array, value: Any, pos: Number) → BooleanParameters
| Parameter | Required | Description |
|---|---|---|
list | Yes | The array to insert into. |
value | Yes | The value to insert. Accepts any type. |
pos | No | The 1-based index at which to insert. Elements at and after this position shift one position toward the end. Defaults to the end of the array. Cannot be 0. |
Edge cases
Array indexes start at
1, not0.poscannot be0.If
posis omitted, the element is appended to the end of the array.
Return value
true on success.
Example
tbl_1 = []
arr_insert(tbl_1, '1') -- tbl_1: ['1']
arr_insert(tbl_1, '3') -- tbl_1: ['1', '3']
arr_insert(tbl_1, '5') -- tbl_1: ['1', '3', '5']
arr_insert(tbl_1, '2') -- tbl_1: ['1', '3', '5', '2']
arr_insert(tbl_1, '6', 1) -- tbl_1: ['6', '1', '3', '5', '2']
str = arr_concat(tbl_1, '')
say(concat('arr_insert:', str))
-- Output: arr_insert:61352arr_remove
Removes the element at a specified position from an array and returns it. See also arr_insert, which inserts elements into an array.
Syntax
arr_remove(list: Array, pos: Number) → AnyParameters
| Parameter | Required | Description |
|---|---|---|
list | Yes | The array to remove from. |
pos | No | The 1-based index of the element to remove. Defaults to the last element. Cannot be 0. |
Edge cases
Array indexes start at
1, not0.poscannot be0.If
posis omitted, the last element is removed and returned.
Return value
The removed element.
Example
tbl_1 = []
arr_insert(tbl_1, '1') -- tbl_1: ['1']
arr_insert(tbl_1, '3') -- tbl_1: ['1', '3']
arr_insert(tbl_1, '5') -- tbl_1: ['1', '3', '5']
arr_insert(tbl_1, '2') -- tbl_1: ['1', '3', '5', '2']
say(concat('arr_remove:', arr_remove(tbl_1, 2)))
-- Output: arr_remove:3Removing the element at index 2 returns '3', and the array becomes ['1', '5', '2'].
arr_sort
Sorts the elements of an array in place, from the first index to the last.
Syntax
arr_sort(list: Array, comp: Function) → BooleanParameters
| Parameter | Required | Description |
|---|---|---|
list | Yes | The array to sort. |
comp | No | A comparison function. Must accept two elements and return true if the first element should precede the second. Defaults to ascending ASCII order. |
Edge cases
If
compis omitted, elements are sorted by ascending ASCII code value. This sort is not stable: elements with equal ASCII rank may swap positions relative to each other.Pass a custom
compfunction when you need numeric ordering or descending order.
Return value
true on success.
Example
The following example demonstrates default ASCII sorting and a custom numeric descending sort.
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))) -- Output: remove:3
str = arr_concat(tbl_1, '')
say(concat('insert:', str)) -- Output: insert:152
arr_sort(tbl_1)
str = arr_concat(tbl_1, '')
say(concat('sort:', str)) -- Output: sort:125
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: sort_comp:521arr_len
Returns the number of elements in an array.
Syntax
arr_len(arr: Array) → NumberParameters
| Parameter | Required | Description |
|---|---|---|
arr | Yes | The array to measure. |
Return value
The element count as a numeric value.
Example
d = []
set(d, 1, 'v1')
say(arr_len(d))
-- Output: 1