how to copy a table with different pointer

Let say if I have a table

table1={}

how can I copy all the content in table1 to table2 that tabel2 will not share the same address with table1.

local t1 = {1,3,5,7,9}
local t2 = {2,4,6,333}
t2[6] = 555 – create hole
local t3 = {11,13,17,19}

local c = table.copy( t1, t2, t3 )

– output: 1, 3, 5, 7, 9, 2, 4, 6, 333, 11, 13, 17, 19
print( table.concat( c, ", " ) )

the example above will share the same address

what I want is:

table1[1]=1   adresss:xxxx2    

table2[1]=1   address:xxxx1  completely different address

I have this code in my projects, I don’t remember where I got it from!

[lua]

function copy(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, _copy(getmetatable(object)))

    end

    return _copy(object)

end

local table1 = copy(table2)

[/lua]

Your method is great to copy some string or number in the table, but if i want to copy like followings, it dosen’t work

[lua]function copy(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, _copy(getmetatable(object)))
end

return _copy(object)
end

h=display.newImage(“a.jpg”)
h.x=100
h.y=200
h.height=50
h.width=30

j=display.newImage(“b.jpg”)
j.x=100
j.y=100
j.height=50
j.width=30

k=display.newImage(“c.jpg”)
k.x=100
k.y=300
k.height=50
k.width=30
T={}
T[1]=h
T[2]=j
T[3]=k

t1=copy(T)

t1[1].x=200
t1[1].y=350[/lua]

I have this code in my projects, I don’t remember where I got it from!

[lua]

function copy(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, _copy(getmetatable(object)))

    end

    return _copy(object)

end

local table1 = copy(table2)

[/lua]

Your method is great to copy some string or number in the table, but if i want to copy like followings, it dosen’t work

[lua]function copy(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, _copy(getmetatable(object)))
end

return _copy(object)
end

h=display.newImage(“a.jpg”)
h.x=100
h.y=200
h.height=50
h.width=30

j=display.newImage(“b.jpg”)
j.x=100
j.y=100
j.height=50
j.width=30

k=display.newImage(“c.jpg”)
k.x=100
k.y=300
k.height=50
k.width=30
T={}
T[1]=h
T[2]=j
T[3]=k

t1=copy(T)

t1[1].x=200
t1[1].y=350[/lua]