Advertisement
Ocawesome101

table.copy

Aug 31st, 2020
1,656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.40 KB | None | 0 0
  1. function table.copy(tbl)
  2.   checkArg(1, tbl, "table")
  3.   local seen = {}
  4.   local function copy(t, to)
  5.     to = to or {}
  6.     for k, v in pairs(t) do
  7.       if type(v) == "table" then
  8.         if not seen[v] then
  9.           seen[v] = {}
  10.           to[k] = seen[v]
  11.           copy(v, seen[v])
  12.         end
  13.       else
  14.         to[k] = v
  15.       end
  16.     end
  17.     return to
  18.   end
  19.   return copy(tbl, {})
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement