0

How to find out whether there is a specific key for a given Lua table through C Apis?

I know that traversing the whole table could achieve this goal, but it's not an efficient method. Is there any better method?

2 Answers 2

3

The function you want is lua_gettable.

First, push the table, then the key you want to check. Then you should be able to do gettable(L, -2) != LUA_TNIL.

3

Simply check if yourtable[yourkey] is nil

You always put a key-value pair into a table. No value, no key.

2
  • Is there any C api that could do the same thing? lua_rawgetp() seems not work.
    – John
    Jan 14, 2021 at 11:44
  • 2
    please update your question and add your code. "seems not to work" is not very informative
    – Piglet
    Jan 14, 2021 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.