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
The tables' metatable will also be copied, as well.
@code local original = {1, 2, greeting = "Hello, World!", fruit = { "apple", "banana", "cherry"}} local copied = table.copy(original) -- `copied` now has copied all elements from `original` @endcodeMerges 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
Be aware that metatables will not be merged as part of this process.
@code() 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 } ]] @endcodeChecks table 'haystack' for anything that matches 'needle'. If found, returns the table's key that
contains the value. If no match is found, returns
'needle' can be any data type. The key returned depends entirely on the structure of 'haystack' and
will be either a
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.
@code() person = { name = "Joe", age = 23, favoriteFoods = { "Pizza", "Nachos", "Chicken Nuggets", }, }; table.print(person); @endcode @code() age: 23 name: "Joe" favoriteFoods: table: 0x02AD35B0 1: "Pizza" 2: "Nachos" 3: "Chicken Nuggets" @endcodeReturn a new
If only 'field' is given, the returned
That makes absolutely no sense, so just look at this example instead.
@code() 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", --]] @endcodePage last updated at 2024-04-02 14:32:32