Table Module
#table.copy table table.copy(table tab)

Actually does a full copy of a table, instead of referencing the original. This also recursively copies sub-tables.

Functions and userdata/C classes cannot be copied directly and so will be left as references to the original data. Built-in Lua data types, such as number, string, table, etc. will be copied.

The tables' metatable will also be copied, as well.

Example:
local original = {1, 2, greeting = "Hello, World!", fruit = { "apple", "banana", "cherry"}} local copied = table.copy(original) -- `copied` now has copied all elements from `original`
#table.merge table table.merge(table a, table b, ...)

Merges the contents of two or more tables into a new table. That is, all of the elements of each table are condensed down to a single table. You may pass as many tables as you would like to this method, but every parameter must be a table. Nested tables are acceptable, but these will be copied instead.

Items keyed by integers (ie. array style) will not retain their keys, but will all be included even if there are duplicates. All other items (ie. key-value pairs, dictionary, map style) may overwrite each other, with the later being given precedence. For example, if the first and second tables passed to table.merge() both contain a name key, then the resulting (merged) table will have its name assigned to the value from the second table's name.

Be aware that metatables will not be merged as part of this process.

Example:
local a = {1, 2, 3} local b = {4, 5} local c = {6} table.print( table.merge(a, b, c) ) -- Should indicate the result including: 1, 2, 3, 4, 5, 6 local alice = {name = "Alice", age = 25, favourite_color = "red"} local bob = {name = "Bob", age = 34} local eve = {name = "Eve", pets = {"cat", "dog"}} local result = table.merge(alice, bob, eve) table.print(result) --[[ `result` now is: { pets = { "cat", "dog" }, favourite_color = "red", name = "Eve", age = 34 } ]]
#table.find number|string table.find(table haystack, needle)

Checks table 'haystack' for anything that matches 'needle'. If found, returns the table's key that contains the value. If no match is found, returns nil.

'needle' can be any data type. The key returned depends entirely on the structure of 'haystack' and will be either a number or string.

Example:
haystack = { "Adam", -- 1 "Jill", -- 2 "Kyle", -- 3 "Steve", -- 4 "Julia", -- 5 }; index = table.find(haystack, "Steve"); -- 'index' should now be 4
#table.print table.print(table tab) table.print(table tab, number depth)

Recursively dump the table to the standard output. This is useful for debugging. When called from Lua, you probably shouldn't include the depth...

Due to how Lua iterates over tables that are not indexed in the format of 1..n (aka a list, ie. if your table uses any keys that are non-sequential starting at 1, or contain any strings), the order in which items are iterated over is unpredictable. In the below example, you will see that 'age' is output before 'name' because of this, however the 'favoriteFoods' table maintains it's order.

Example:
person = { name = "Joe", age = 23, favoriteFoods = { "Pizza", "Nachos", "Chicken Nuggets", }, }; table.print(person);
Example output:
age: 23 name: "Joe" favoriteFoods: table: 0x02AD35B0 1: "Pizza" 2: "Nachos" 3: "Chicken Nuggets"
#table.lists table table.lists(table tab, string field) table table.lists(table tab, string field, string key)

Return a new table (list or dictionary) based on the input table and requested key/value. 'tab' must be a table of tables!

If only 'field' is given, the returned table will be an array of all values for keyed with that name. You may key the returned table with the value of the variable specified by 'key'.

That makes absolutely no sense, so just look at this example instead.

Example:
local tab = { [1] = { name = "Bob", age = 30, id = 123456 }, [2] = { name = "Jane", age = 27, id = 654321 }, } local newTab = table.lists(tab, 'name'); local newTab2 = table.lists(tab, 'id', 'name'); --[[ newTab should now contain: [1] = "Bob", [2] = "Jane", newTab2 should now contain: [123456] = "Bob", [654321] = "Jane", --]]

Page last updated at 2024-04-02 14:32:32


Copyright 2024