lua-users home
lua-l archive

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


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
}
On 14 Jan 2021, 14:55 +0300, Francisco Olarte <folarte@peoplecall.com>, wrote:
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