The recursive table solution seems a bit over-complicated anyhow. I always created this solution when I wanted to copy tables:
local function DeepCopy(Table)
local Copy = {}
for k,v in pairs(Table) do
if typeof(v) == "table" then
Copy[k] = DeepCopy(v)
else
Copy[k] = v
end
end
return Copy
end
Or, the space-saving method I use:
local function DeepCopy(Table)
local Copy = {}
for k,v in pairs(Table) do
Copy[k] = typeof(v) == "table" and DeepCopy(v) or v
end
return Copy
end
Or, if you really wanted to save space:
As incapaxx pointed out to me, foreach is deprecated and shouldn’t be used, but here’s the code nonetheless:
local function DeepCopy(Table)
local Copy = {}
table.foreach(Table, function(k,v) Copy[k] = typeof(v) == "table" and DeepCopy(v) or v end)
return Copy
end
I’ve fixed the syntax issues with the page, and while I’d like to take the time to do a proper re-write of this article I’ll have to put that on the backburner. This article was among the many old wiki-migrated articles.