lua-users home
lua-l archive

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

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


>Assuming table is at index 1
Index 1?
In general, the index of the stack is a negative number, e.g -1, -2, and so on.
On Thu, Jan 14, 2021 at 10:11 PM Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>
> > One more way with lua_isnil.
> >
> > lua_pushstring(L, "key");
> > lua_rawget(L, 1); // Assuming table is at index 1
> > if (lua_isnil(L, -1)) {
> > // It's nil
> > } else {
> > // It's something else
> > }
>
> Or else:
>
> lua_pushstring(L, "key");
> if (lua_rawget(L, 1) == LUA_TNIL) { // Assuming table is at index 1
> // It's nil
> } else {
> // It's something else
> }
>
> -- Roberto

AltStyle によって変換されたページ (->オリジナル) /