我们使用此网站上的 Cookie 增强您的用户体验
Collapse Sidebar


This library provides generic functions for table/array manipulation, providing all its functions inside the global table variable. Most functions in the table library assume that the table represents an array or a list. For these functions, the “length” of a table means the result of the length operator.

string table.concat ( array t, string sep = "", int i = 1, int j = #t )

Given an array where all elements are strings or numbers, returns the string t[i] … sep … t[i+1] … sep … t[j]. The default value for sep is an empty string, the default for i is 1, and the default for j is #t. If i is greater than j, returns the empty string.

void table.foreach ( array t, function f )

Iterates over the provided table, passing the key and value of each iteration over to the provided function.

void table.foreachi ( array t, function f )

This is similar to table.foreach() except that index-value pairs are passed, not key-value pairs.

number table.getn ( array t )

Returns the number of elements in the table passed.

void table.insert ( array t, number pos = #t+1, Variant value )

Appends the provided value to the end of array t. The optional pos value defaults to #t+1, meaning that value is inserted at the end of array t unless otherwise specified.

Variant table.remove ( array t, number pos = #t )

Removes from array t the element at position pos, returning the value of the removed element. When pos is an integer between 1 and #t, it shifts down the elements t[pos+1], t[pos+2], …, t[#t] and erases element t[#t]. The index pos can also be 0 when #t is 0 or #t+1; in those cases, the function erases the element t[pos].

void table.sort ( array t, function comp = nil )

Sorts elements of array t in a given order, from t[1] to t[#t]. If comp is given, then it must be a function that receives two elements and returns true when the first element must come before the second in the final order (so that not comp(t[i+1],t[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.