@EHO
I just did a quick test on your data, and the deepcopy works just fine. Using a couple utility functions, it’s pretty easy to verify…
-- First a couple utility functions...
--
-- Creates a complete/deep copy of the data
function deepCopy(object)
local lookup\_table = {}
local function \_copy(object)
if type(object) ~= "table" then
return object
elseif lookup\_table[object] then
return lookup\_table[object]
end
local new\_table = {}
lookup\_table[object] = new\_table
for index, value in pairs(object) do
new\_table[\_copy(index)] = \_copy(value)
end
return setmetatable(new\_table, getmetatable(object))
end
return \_copy(object)
end
--
-- Giant table printing function... deals with recursion and stuffs...
--
function table.show(t, name, indent)
local cart -- a container
local autoref -- for self references
local function isemptytable(t) return next(t) == nil end
local function basicSerialize (o)
local so = tostring(o)
if type(o) == "function" then
local info = debug.getinfo(o, "S")
-- info.name is nil because o is not a calling level
if info.what == "C" then
return string.format("%q", so .. ", C function")
else
-- the information is defined through lines
return string.format("%q", so .. ", defined in (" ..
info.linedefined .. "-" .. info.lastlinedefined ..
")" .. info.source)
end
elseif type(o) == "number" or type(o) == "boolean" then
return so
else
return string.format("%q", so)
end
end
local function addtocart (value, name, indent, saved, field)
indent = indent or ""
saved = saved or {}
field = field or name
cart = cart .. indent .. field
if type(value) ~= "table" then
cart = cart .. " = " .. basicSerialize(value) .. ";\n"
else
if saved[value] then
cart = cart .. " = {}; -- " .. saved[value]
.. " (self reference)\n"
autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
else
saved[value] = name
--if tablecount(value) == 0 then
if isemptytable(value) then
cart = cart .. " = {};\n"
else
cart = cart .. " = {\n"
for k, v in pairs(value) do
k = basicSerialize(k)
local fname = string.format("%s[%s]", name, k)
field = string.format("[%s]", k)
-- three spaces between levels
addtocart(v, fname, indent .. " ", saved, field)
end
cart = cart .. indent .. "};\n"
end
end
end
end
name = name or "\_\_unnamed\_\_"
if type(t) ~= "table" then
return name .. " = " .. basicSerialize(t)
end
cart, autoref = "", ""
addtocart(t, name, indent)
return cart .. autoref
end
--
-- Top level call to print a table...
--
function printTable( t, label )
print(table.show(t, label))
end
--------------------------
local map = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
}
local map2 = deepCopy(map) -- This is a complete copy (not a reference...)
map2[1][1] = 9 -- Test values, should ONLY be changed in map2 (the deepCopy)
map2[1][2] = 9
map2[1][3] = 9
printTable( map, "Original Map")
printTable( map2, "deepCopy of Map")
print("Done.")
Best of luck. [import]uid: 79933 topic_id: 32957 reply_id: 131143[/import]