lua-users home
lua-l archive

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


On 13/03/2008, Alexandr Leykin <leykina@gmail.com> wrote:
>
> >
> > Perhaps you could be more specific about your concern or problem.
> >
> > - Jim
> >
> >
> I want to get "is_key(table,key)->boolean" function, but not use full select
> (using pairs or select operator)
>

Just index the table using the key and check if the value is non-nil:

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function is_key(table, key) return table[key] ~= nil end
> t = {a = 4, b = 3, d = 9}
> = is_key(t, "a")
true
> = is_key(t, "b")
true
> = is_key(t, "c")
false
> = is_key(t, "d")
true