without metatables:
Code:
function tablecopy(tbl)
if type(tbl) ~= "table" then return tbl end
local t = {}
for i,v in pairs(tbl) do
t[i] = tablecopy(v)
end
table.setn(t, table.getn(tbl))
return t
end
with metatables (shallow copy):
Code:
function tablecopy(tbl)
if type(tbl) ~= "table" then return tbl end
local t = {}
for i,v in pairs(tbl) do
t[i] = tablecopy(v)
end
table.setn(t, table.getn(tbl))
return setmetatable(t, getmetatable(tbl))
end
with metatables (deep copy):
Code:
function tablecopy(tbl)
if type(tbl) ~= "table" then return tbl end
local t = {}
for i,v in pairs(tbl) do
t[i] = tablecopy(v)
end
table.setn(t, table.getn(tbl))
return setmetatable(t, tablecopy(getmetatable(tbl)))
end
I think. the main difference from Dhargo's is the setn and the metatables. You'll probably just want the shallow metatable copy.