lua-users home
lua-l archive

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



On 19-Apr-05, at 6:31 PM, Matthew Percival wrote:

	I've written a simple function to step through each key in the table,
and if it's not the one I want to remove add it to a the new table. Not very clean, but it does the job. I was wondering if there were a better
method for this already built into the language that I've missed
somewhere, or isn't in the wiki.

table[key] = nil

I'd imagine I could clean it up a
little with recursion, but I was wondering if there were a simple way to
save it with chunks like I can do with loading it.

Recursion would undoubtedly lead to cleaner code.

There is no built-in facility for saving tables; you could search the mailing list archives for various discussions on why there should or should not be one. What makes it challenging is that a table could contain any Lua object as either a key or a value (except for nil and NaN), and could also contain multiple references to the same object. Not all objects have a printable representation (userdata, in particular, do not even have an obvious marshalled representation), and it is not particularly clear what to do about multiple references, particularly as the marshalled output may consist of a number of "pickles" each produced independently.

Various solutions have been proposed, but it seems that there is no consensus about a general solution. No doubt a number of people will now propose their favourites :)