Game Development Reference
In-Depth Information
end
res[k] = v
end
setmetatable(res,mt)
return res
end
Note
If you have cyclic references in your tables, these are not handled by this function.
Copying Array Components
In some cases you might want to only copy the array component, not the entire table, as follows:
function copyList(table 1)
local res = {}
for i,j in ipairs(table 1) do
res[i] = j
end
return res
end
Just in case you think that the two preceding functions are the same, note that in the first function
we are using the function pairs , while in the second one we are using ipairs .
Copying Non-Array Components
If you need to copy the non-array component and leave the array alone, then you can do so with the
following:
function copyHash(table 1)
local res = {}
local size = #table 1
for i, j in pairs(table 1) do
if type(j) ~= "number" or i<= 0 or i> size then
res[i] = j
end
end
return res
end
 
 
Search WWH ::




Custom Search