functionshallowcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table'then copy = {} for orig_key, orig_value inpairs(orig) do copy[orig_key] = orig_value end else-- number, string, boolean, etc copy = orig end return copy end
DeepCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functiondeepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table'then copy = {} for orig_key, orig_value innext, orig, nildo copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else-- number, string, boolean, etc copy = orig end return copy end