Whenver I try to do
Code: Select all
table2=table1
I've thought of using serialization libs to serialize a table and then load the string into the new table but mayber there's another way ..?
Code: Select all
table2=table1
Code: Select all
a = {1,2,3}
b = {}
for k, v in ipairs(a) do
b[k] = v
end
Code: Select all
function DeepCopy( Table, Cache ) -- Makes a deep copy of a table.
if type( Table ) ~= 'table' then
return Table
end
Cache = Cache or {}
if Cache[Table] then
return Cache[Table]
end
local New = {}
Cache[Table] = New
for Key, Value in pairs( Table ) do
New[DeepCopy( Key, Cache)] = DeepCopy( Value, Cache )
end
return New
end
I'd already thought of that but, as you said, tables within tables are the problem and if checks would not be the solution, but apparently Robin found a way.(or someone else did because I think I've seen his solution somewhere else)jangsy5 wrote:You have to note that it'll reference values that are tables so you need to clone those too, probably using if statement to check.
Thank you very much for sharing, and thank you Robin for coding thisdavisdude wrote:*Made by RobinCode: Select all
function DeepCopy( Table, Cache ) -- Makes a deep copy of a table. if type( Table ) ~= 'table' then return Table end Cache = Cache or {} if Cache[Table] then return Cache[Table] end local New = {} Cache[Table] = New for Key, Value in pairs( Table ) do New[DeepCopy( Key, Cache)] = DeepCopy( Value, Cache ) end return New end
Users browsing this forum: Google [Bot] and 8 guests