[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?
- From: Francisco Olarte <folarte@...>
- Date: Thu, 14 Jan 2021 12:54:10 +0100
On Thu, Jan 14, 2021 at 12:43 PM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
> >Simply check if yourtable[yourkey] is nil
> This is a Lua script.
> How to achieve this goal(i.e the subject) by C api?
Translate to the C API, push the table, push the key, lookup using
https://www.lua.org/manual/5.4/manual.html#lua_gettable
You may need some more movements, depending on where you have your
table and key stored, i.e., you may need something like this for
globals:
lua_getglobal(L, "yourtable"); /* Table is at -1 */
lua_getglobal(L, "yourkey"); /* Table is at -2, key at -1 */
lua_gettable(L, -2); /* Table still at -2, value at -1 */
And some error checking if you deem it neccessary.
FOS