All Products
Search
Document Center

Server Load Balancer:Array functions

Last Updated:Apr 30, 2025

This topic describes the syntax, features, parameters, and return values of array functions. This topic also provides examples of these functions.

arr_concat

Item

Description

Syntax

arr_concat(tbl, [sep])

Feature

Concatenates strings in a specified table by using a specified character.

Parameters

  • tbl: the table that contains the arrays to be concatenated.

  • sep: the character that is used to concatenate strings. By default, empty strings are used. This parameter is optional.

Example

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

Return values

A string that is concatenated by using the specified character. In this example, t1&t2&t3 is returned.

arr_insert

Item

Description

Syntax

arr_insert(list, value, [pos])

Feature

Inserts elements into an array.

Parameters

  • list: the array into which you want to insert elements.

  • value: any type.

  • (Optional) pos: a numeric value, excluding 0. The index of the array specified by list starts from 1. The element is inserted into the position specified by pos. Elements that follow the inserted element are moved towards the end of the array for one position. If you do not set the pos parameter, the element is inserted into the end of the array.

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))

Return values

A value of true. In this example, arr_insert:61352 is returned.

arr_remove

Item

Description

Syntax

arr_remove(list, [pos])

Feature

Removes elements at the specified position from an array and returns the removed elements.

Parameters

  • list: the array from which you want to remove elements.

  • (Optional) pos: a numeric value, excluding 0. The index of the array specified by list starts from 1. If you do not set the pos parameter, the element that is at the end of the array is removed and returned.

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)))

Return values

The element that is removed from the specified array. In this example, arr_remove:3 is returned.

arr_sort

Item

Description

Syntax

arr_sort(list, [comp])

Feature

Sorts elements from the beginning to the end of the index in an array in a specified order.

Parameters

  • list: the array whose elements you want to sort.

  • (Optional) comp: the sorting algorithm, which must be a function.

    • If you set the comp parameter, it must be a function that supports two elements in the specified array as its parameters. A value of true is returned if the first element is placed before the second element.

    • If you do not set the comp parameter, the elements are sorted based on ASCII codes in ascending order. However, this sorting algorithm may change the original order of two elements that have the same rank in the ASCII table.

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))

Return values

A value of true. In this example, the following values are returned:

remove:3
insert:152
sort:125
sort_comp:521

arr_len

Item

Description

Syntax

arr_len(arr)

Feature

Counts the number of elements in an array.

Parameters

arr: the array.

Example

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

Return values

The number of elements in the specified array. Data type: numeric. In this example, 1 is returned.