Created
April 30, 2018 02:35
-
-
Save revolucas/184aec7998a6be5d2f61b984fac1d7f7 to your computer and use it in GitHub Desktop.
Merge two tables recursively without worry of 'stack overflow'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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