table

顯示已棄用項目

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.

概要

函式

  • clear(table : table):()

    Sets all keys in the given table to nil.

  • Returns a shallow copy of the provided table.

  • concat(t : Array,sep : string,i : number,j : number):string

    Returns the given range of table elements as a string where each element is separated by the given separator.

  • create(count : number,value : Variant):table

    Returns a new table populated with many instances of the specified value.

  • find(haystack : table,needle : Variant,init : number):Variant

    Returns the index of the first occurrence of needle within haystack starting from init.

  • Makes the given table read-only.

  • insert(t : Array,pos : number,value : Variant):()

    Inserts the provided value to the target position of the array.

  • insert(t : Array,value : Variant):()

    Appends the provided value to the end of the array.

  • Returns true if the given table is frozen and false if it isn't frozen.

  • Returns the maximum numeric key of the provided table, or zero if the table has no numeric keys.

  • move(src : table,a : number,b : number,t : number,dst : table):table

    Copies the specified range of elements from one table to another.

  • pack(values... : Variant):Variant

    Returns a new table containing the provided values.

  • remove(t : Array,pos : number):Variant

    Removes the specified element from the array, shifting later elements down to fill in the empty space if possible.

  • sort(t : Array,comp : function):()

    Sorts table elements using the provided comparison function or the < operator.

  • unpack(list : table,i : number,j : number):Tuple

    Returns all elements from the given list as a tuple.

函式

clear

()

Sets the value for all keys within the given table to nil. This causes the # operator to return 0 for the given table. The allocated capacity of the table's array portion is maintained, which allows for efficient re-use of the space.


local grades = {95, 82, 71, 92, 100, 60}
print(grades[4], #grades) --> 92, 6
table.clear(grades)
print(grades[4], #grades) --> nil, 0
-- If grades is filled again with the same number of entries,
-- no potentially expensive array resizing will occur
-- because the capacity was maintained by table.clear.

This function does not delete/destroy the table provided to it. This function is meant to be used specifically for tables that are to be re-used.

參數

table: table

The table whose keys will be cleared.

返回

()

clone

Returns an unfrozen shallow copy of the provided table.

參數

The table to be cloned.

返回

The clone of the provided table.

concat

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.

參數

The table that will be converted into a string.

sep: string

The string that will be concatenated between each entry in the table.

The starting index of the table concatenation.

預設值:1

The ending index of the table concatenation.

返回

create

Creates a table with the array portion allocated to the given number of elements, optionally filled with the given value.


local t = table.create(3, "Roblox")
print(table.concat(t)) --> RobloxRobloxRoblox

If you are inserting into large array-like tables and are certain of a reasonable upper limit to the number of elements, it's recommended to use this function to initialize the table. This ensures the table's array portion of its memory is sufficiently sized, as resizing it can be expensive. For small quantities this is typically not noticeable.

參數

count: number
value: Variant

返回

find

Variant

Within the given array-like table haystack, find the first occurrence of value needle, starting from index init or the beginning if not provided. If the value is not found, nil is returned.

A linear search algorithm is performed.


local t = {"a", "b", "c", "d", "e"}
print(table.find(t, "d")) --> 4
print(table.find(t, "z")) --> nil, because z is not in the table
print(table.find(t, "b", 3)) --> nil, because b appears before index 3

參數

haystack: table
needle: Variant
init: number

返回

Variant

freeze

This function makes the given table read-only, effectively "freezing" it in its current state. Attempting to modify a frozen table throws an error.

This freezing effect is shallow, which means that you can write to a table within a frozen table. To deep freeze a table, call this function recursively on all of the descending tables.

參數

The table to be frozen.

返回

The frozen table.

insert

()

Inserts the provided value to the target position of the array.

參數

The table that is being appended to.

pos: number

The position at which the value will be inserted.

value: Variant

The value that will be appended to the table.

返回

()

insert

()

Appends the provided value to the end of the array.

參數

The table that is being appended to.

value: Variant

The value that will be appended to the table.

返回

()

isfrozen

This function returns true if the given table is frozen and false if it isn't frozen. You can freeze tables using table.freeze().

參數

The table to check.

返回

Whether the table is frozen from table.freeze().

maxn

Returns the maximum numeric key of the provided table, or zero if the table has no numeric keys. Gaps in the table are ignored.

參數

返回

move

Copies elements in table src from src[a] up to src[b] into table dst starting at index t. Equivalent to the assignment statement dst[t], ..., dst[t + (b - a)] = src[a], ..., src[b].

The default for dst is src. The destination range may overlap with the source range.

Returns dst for convenience.

參數

src: table

Source table.

Start copying at src[a].

Copy up to and including src[b].

Copy into dst[t], ....

dst: table

Destination table.

預設值:src

返回

dst, for convenience.

pack

Variant

Returns a new table with all arguments stored into keys 1, 2, etc. and with a field "n" with the total number of arguments. Note that the resulting table may not be a sequence.


local t = table.pack(1, 2, 3)
print(table.concat(t, ", ")) --> 1, 2, 3

參數

values...: Variant

返回

Variant

remove

Variant

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]. If the pos parameter is not provided, pos defaults to the length of the table removing the last element.

參數

The table that is having an element removed.

pos: number

The index of the element being removed.

返回

Variant

sort

()

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.

The error invalid order function for sorting is thrown if both comp(a, b) and comp(b, a) return true.

If comp is not given, then the standard Luau operator < is used instead.

參數

comp: function

An optional comparison function to be used when comparing elements in the table. This function receives two elements, and should return true if the first element should be sorted before the second in the final order.

預設值:nil

返回

()

unpack

Returns the elements from the given list. By default, i is 1 and j is the length of list.

Note that this same functionality is also provided by the global unpack() function.

參數

list: table

The list of elements to be unpacked.

The index of the first element to unpack.

預設值:1

The index of the last element to unpack.

預設值:#list

返回