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.

#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.

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.

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.

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 2018-09-25 20:48:46

Copyright 2023