Not a help request, but a contribution that the community may find useful. I’ve written a module that contains some useful routines that help in debugging, table manipulation, and so on… So far, there’s only four, but I plan to add more when the need arises.
--
--
-- A collection of useful utility routines.
--
--
local utilityMod = {}
-- Recursively copies a table into a new table and
-- returns it.
utilityMod.revursiveCopy = function(dataTable)
local tableCopy = {}
for index, value in pairs(dataTable) do
if type(value) == "table" then
value = utilityMod.recursiveCopy(value)
end
tableCopy[index] = value
end
return tableCopy
end
-- Merges data from table2 into table1, overwriting values that
-- share the same index. Returns the merged table. The input
-- tables are not modified.
utilityMod.mergeTable = function(table1, table2)
local tableCopy = {}
tableCopy = utilityMod.revursiveCopy(table1)
for index, value in pairs(table2) do
tableCopy[index] = value
end
return tableCopy
end
-- Prints out a table.
utilityMod.printTable = function(tableData)
table.foreach(tableData, function(key, value)
print(key, "=", value)
end)
end
-- Similar to print table, but prints it recursively. Nested
-- tables are indented. Tab is used for recursive indenting.
-- Do not use tab when calling.
utilityMod.printTableRecursive = function(tableData, tab)
if tab == nil then
tab = ""
end
for key, value in pairs(tableData) do
if type(value) == "table" then
print(string.format(tab) .. ">> Subtable: ", key)
utilityMod.printTableRecursive(value, tab .. "\t")
else
print(string.format(tab), key, "=", value)
end
end
end
return utilityMod