lua-users home
lua-l archive

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




On Thu, Mar 27, 2008 at 3:17 PM, Lua Newbie <luanewbie@yahoo.com> wrote:
I've googled and searched the archive to no avail ...

Is there a way to detect duplicate field names in a table?

Suppose I want users to be able to configure an app using lua.  They create a table like:

config={key1=val1,key2=val2}

I want to detect and reject any duplicate keys (eg key1=key2). 

Take a look at "Read-only tables" in Programming in Lua - that example can be adapted to do what you want.

http://www.lua.org/pil/13.4.5.html

The only difference: Instead of throwing an error every time you detect an update, first check to see if the value you're trying to set is nil. If it's not nil, throw the error - but if there's nothing there, do the assignment yourself to allow the update to go through.

Hope this helps,

Evan