Skip to content

Instantly share code, notes, and snippets.

@revolucas
Created April 30, 2018 02:35
Show Gist options
  • Save revolucas/184aec7998a6be5d2f61b984fac1d7f7 to your computer and use it in GitHub Desktop.
Save revolucas/184aec7998a6be5d2f61b984fac1d7f7 to your computer and use it in GitHub Desktop.
Merge two tables recursively without worry of 'stack overflow'
function table_merge(into,from)
local stack = {}
local node1 = into
local node2 = from
while (true) do
for k,v in pairs(node2) do
if (type(v) == "table" and type(node1[k]) == "table") then
table.insert(stack,{node1[k],node2[k]})
else
node1[k] = v
end
end
if (#stack > 0) then
local t = stack[#stack]
node1,node2 = t[1],t[2]
stack[#stack] = nil
else
break
end
end
return into
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment