Make a duplicate copy of a multidimensional array - similar to Flash slice()

I am porting some code from Flash and need to make a temporary duplicate of a multidimensional array and wondered if Lua has anything that can help?

In Flash, I would use

myArrayCopy = myArray.slice()

May have found a solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function deepcopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end
views:1432 update:2011/10/6 9:28:12
corona forums © 2003-2011