Difference between revisions of "Table Addon"

From SolarStrike wiki
Jump to: navigation, search
m
 
Line 13: Line 13:
  
 
== print ==
 
== print ==
'''table.print(table tab[, number depth])'''
+
'''table.print(table tab)'''
 +
 
 +
'''table.print(table tab, number depth)'''
  
 
Recursively dump the table to the standard output. This is useful for debugging.
 
Recursively dump the table to the standard output. This is useful for debugging.
 
When called from Lua, you probably shouldn't include the depth...
 
When called from Lua, you probably shouldn't include the depth...
 +
 +
 +
== lists ==
 +
'''table.lists(table tab, string key)'''
 +
 +
'''table.lists(table tab, string key, string value)'''
 +
 +
Return a new table (plain array or dictionary) based on the input table and requested key/value. 'tab' must be a table of tables!
 +
 +
If only 'key' is given, the returned table will be an array of just the contents of subvalues of keys. If 'key' and 'value' are given, a new dictionary is returned, where the new key is the value of 'key' and the new value is the value of 'value'.
 +
 +
That makes absolutely no sense, so just look at this example instead.
 +
<source lang="lua">
 +
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');
 +
</source>
 +
 +
'newTab' would now contain:
 +
<nowiki>
 +
[1] = "Bob",
 +
[2] = "Jane",
 +
</nowiki>
 +
 +
'newTab2' would now contain:
 +
<nowiki>
 +
[123456] = "Bob",
 +
[654321] = "Jane",
 +
</nowiki>

Latest revision as of 18:46, 25 February 2016

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.


find

table table.find(table haystack, number|string 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.


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


lists

table.lists(table tab, string key)

table.lists(table tab, string key, string value)

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

If only 'key' is given, the returned table will be an array of just the contents of subvalues of keys. If 'key' and 'value' are given, a new dictionary is returned, where the new key is the value of 'key' and the new value is the value of 'value'.

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' would now contain:

	[1] = "Bob",
	[2] = "Jane",

'newTab2' would now contain:

	[123456] = "Bob",
	[654321] = "Jane",