lua-users home
lua-l archive

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


Hi, I've been using Lua for an application I'm developing, and I have a
question about copying a nested table structure.

I have a system using templates. The application users will take an
existing table, and clone it to create a copy which they then can tweak.

Example:

Template

Pet = 
{
	Location={ Type="Pound", Region="Southwest" },
	Name="",
	Commands={	Fetch, Grovel, Growl }
}

User 

Monkey = 
{
	From=Pet,
	Name="MonkeySee",
	SpecialActions={	Warble, Swoon	},
	Move=function()
}

What I want to do is make a copy of the Pet template, and have Monkey
inherit the *copy* of Pet.

I have been able to copy root level values, but nested tables are not
copied, just the references. The problem above is if I iterate through
the Pet table and copy everything, if I change a value in the nested
tables, it changes it for the underlying Pet table because both Pet and
Monkey point to the same nested table.

Is there any facility that will let me make an independent copy of a
table and all its nested tables (yes, I understand the circular
reference problem that could occur, so I'll need to be careful).

Thanks for any insight!
Tim