table.lua


table

Basic table operations that every Lua programmer needs to know.
table.clear(t)Removes all values from the given table.
table.copy(src, dest)Copies the contents from one table to another. Overwrites existing elements in the destination table. Preserves table cycles
table.count(haystack, needle)Counts the number of elements in a table. Optionally counts the number of occurrences of a specific value.
table.empty(t)Checks if the given table is empty.
table.find(haystack, needle, offset)Finds the first occurrence of a given value in a table.
table.random(t)Returns a random element from a table.
table.reverse(src, dest)Reverses all of the elements in a table.
table.rfind(haystack, needle, offset)Finds the last occurrence of a given value in a table.
table.shuffle(src, dest)Randomly shuffles the elements in a table.
table.unique(t)Checks for duplicate elements in a table.

table.clear(t)

Removes all values from the given table.
tablet
The table that will be cleared

table.copy(src, dest)

Copies the contents from one table to another. Overwrites existing elements in the destination table. Preserves table cycles
tablesrc
Source table
tabledest (optional)
Destination table
Returns
table Resulting table

table.count(haystack, needle)

Counts the number of elements in a table. Optionally counts the number of occurrences of a specific value.
tablehaystack
The table that will be searched
valueneedle (optional)
Search value
Returns
number Number of occurrences (when the optional value is specified)
number Total number of elements

table.empty(t)

Checks if the given table is empty.
tablet
Table that will be checked
Returns
boolean True if empty or false otherwise

table.find(haystack, needle, offset)

Finds the first occurrence of a given value in a table.
tablehaystack
Numerically-indexed table
valueneedle
Search value
numberoffset (1)
Starting index
Returns
number Numeric index or nil

table.random(t)

Returns a random element from a table.
tablet
Numerically-indexed table
Returns
value Random element
number Index of the random element

table.reverse(src, dest)

Reverses all of the elements in a table.
tablesrc
Numerically-indexed table
tabledest (optional)
Destination table
Returns
table Reversed table

table.rfind(haystack, needle, offset)

Finds the last occurrence of a given value in a table.
tablehaystack
Numerically-indexed table
valueneedle
Search value
numberoffset
Starting index
Returns
number Numeric index or nil

table.shuffle(src, dest)

Randomly shuffles the elements in a table.
tablesrc
Numerically-indexed table
tabledest (optional)
Destination table
Returns
table Shuffled table

table.unique(t)

Checks for duplicate elements in a table.
tablet
The table that will be checked
Returns
boolean True if all elements are unique