Table addon

From SolarStrike wiki
Revision as of 21:52, 1 August 2011 by Elverion (talk | contribs) (Created page with '== table.copy == '''table table.copy(table A)''' Creates a copy of table 'A' and returns the new instance. '''Example''' <source lang="lua"> local tab = {1, 2, 3}; local tab2 =…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

table.copy

table table.copy(table A)

Creates a copy of table 'A' and returns the new instance.

Example

local tab = {1, 2, 3};
local tab2 = table.copy(tab);
-- tab and tab2 are two different tables that contain the same data


table.contains

bool table.copy(table, value)

Returns true if the table has a key-value pair with the given value.

Example

local tab = {1, 2, 3};
print("Tab contains 1: ", table.contains(tab, 1));
-- This should print: Tab contains 1: true


table.print

bool table.copy(table, depth)

Prints out all key-value pairs within the table, including nested tables.

Example

local tab = {"Test1" = "Test1 value", "Test2" = "Test2 value", "Test table" = {1, 2, 3}};
table.print(tab);


table.save

string table.save(table) string table.save(table, bool) bool table.save(table, filename)

This function allows you to save a table in a Lua compatible way that can then be loaded again using table.load(). Be aware that userdata types and upvalues will not be saved.

If the second value passed to this function is true, io.tmpfile() will be used, making this ideal for larger data. If the second value is a string representing the wanted filename, this function will save the data to that file and return true or false depending on whether or not it was successful.

Example

local tab = {1, 2, 3};
local str = table.save(tab); -- str is now a serialized string of tab
if( table.save(tab, "test.txt") ) then
print("File successfully saved.");
end


table.load

table table.load(string) table table.load(filename)

Loads a table that was saved using table.save().

Example

local tab = {1, 2, 3};
local str = table.save(tab, str);
local tab2 = table.load(str); -- tab2 should now be a copy of tab.