Skip to content

Instantly share code, notes, and snippets.

@kaeza
Last active April 5, 2019 20:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kaeza/ba0b458b430d353dd6f0 to your computer and use it in GitHub Desktop.
Lua - Recursive table copy
local function copy(t, seen)
local nt = { }
seen = seen or { }
seen[t] = nt
for k, v in pairs(t) do
if type(v) == "table" then
if seen[v] then
nt[k] = seen[v]
else
nt[k] = copy(v, seen)
end
else
nt[k] = v
end
end
return nt
end
local t = { a=1, b=2, }
t.c = t
local nt = copy(t)
t.a=4
print(nt.c.c.c.c.a) --> 1
print(nt.c.c.c.c.b) --> 2
print(nt.b) --> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment