lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Safe... as long as there are no cyclic dependencies.

local t = {}
t.a = t

This kind of thing is discussed in PIL when it talks about serialization.

Am I the only person who thinks there should be shallow and deep copy functions in the table library? Or at least in the Wiki? Perhaps even three functions... shallow, deep, and deep with refs.

Mike



Hercinger Viktor wrote:
Hi!

Recusively it can be done like this:

function dup(tab)
    local res = {}

    if type(tab) ~= "table" then return tab end

    for k, v in tab do res[dup(k)] = dup(v) end

    return res
end

And it's kind of safe too :)

Chris wrote:
Are there any tricks to insert all the keys and values of one table into another?

For example:

t = {}
t.a = "a"
t.b = "b"
t.c = "c"

u = { unpack(t), d="d", e="e" }

I know you can't use unpack except on numbered index values. Is my only choice a table.foreach with an insert of each value from `t' into `u'?

--
// Chris