Re: Lua Basics: table duplication?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Lua Basics: table duplication?
- From: Adrian Sietsma <adrian_groups@...>
- Date: 2005年8月29日 14:33:14 +1000
William Trenker wrote:
Sometimes I want to make a duplicate copy of a table, not just assign
another reference to it. Is this the standard way:
a={1,2,3, m="m", "n" , o={4}, p = function() end}
b={}
table.foreach(a, function(k,v) b[k]=v end)
close, but you'll still end up with references to child tables, rather than
copies. this may be ok for you, otherwise you have to recurse child tables
function clone(node)
if type(node) ~= "table" then return node end
local b = {}
table.foreach(node, function(k,v) b[k]=clone(v) end)
return b
end
a={1,2,3, m="m", "n" , o={4}, p = function() end}
b=clone(a)
(and that still will only copy references to userdata and functions)
Adrian