All Products
Search
Document Center

CDN:Array functions

Last Updated:Apr 01, 2026

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

ParameterRequiredDefaultDescription
tblYesThe array whose elements you want to concatenate.
sepNo"" (empty string)The separator inserted between elements.

Example

d = ['t1','t2','t3']
say(arr_concat(d, '&'))
-- Output: t1&t2&t3

arr_insert

Inserts an element into an array at a specified position.

Syntax: arr_insert(list, value, [pos])Boolean

ParameterRequiredDefaultDescription
listYesThe array to insert into.
valueYesThe element to insert. Accepts any data type.
posNoEnd of arrayThe 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:61352

arr_remove

Removes an element from an array at a specified position and returns it.

Syntax: arr_remove(list, [pos])Any

ParameterRequiredDefaultDescription
listYesThe array to remove from.
posNoLast elementThe 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:3

arr_sort

Sorts the elements of an array in place.

Syntax: arr_sort(list, [comp])Boolean

ParameterRequiredDefaultDescription
listYesThe array to sort.
compNoANSII ascendingA comparator function that determines element order.

Comparator behavior

comp(a, b) return valueSort order
comp not providedElements are sorted by ANSII code in ascending order. Elements with equal ANSII values may be reordered.
truea is placed before b.
falseb is placed before a.
Note: The comp function receives two elements from list as arguments. To sort in descending order, return true when a > 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:521

arr_len

Returns the number of elements in an array.

Syntax: arr_len(arr)Number

ParameterRequiredDescription
arrYesThe array to count.

Example

d = []
set(d, 1, 'v1')
say(arr_len(d))
-- Output: 1