Values & References10 / 10
Up next

Shallow copy shares nested tables

local function shallow_copy(t)
  local out = {}
  for k, v in pairs(t) do out[k] = v end
  return out
end
local grid = {{1, 2}, {3, 4}}
local copy = shallow_copy(grid)
-- the inner tables are still the SAME objects
table.insert(copy[1], 99)
print(#grid[1])
What you're solving

nested tables stay shared after a shallow copy