What is the C interface for Lua to access key / value table pairs?

In Lua, using the C interface, given a table, how do I iterate over the key / value pairs of a table?

In addition, if some members of the table are added using arrays, do I need a separate loop to iterate over them, or is there one way to iterate, although these members are simultaneously with key / value pairs?

+3
source share
2 answers

lua_next()is the same as the Lua function next()that the function uses pairs(). It iterates over all members, both in the array part and in the hash part.

ipairs(), lua_objlen() , #. lua_rawgeti() .

+6

, lua_next(). , , .

:

:

/* table is in the stack at index 't' */
lua_pushnil(L);  /* first key */
while (lua_next(L, t) != 0) {
   /* uses 'key' (at index -2) and 'value' (at index -1) */
   printf("%s - %s\n",
          lua_typename(L, lua_type(L, -2)),
          lua_typename(L, lua_type(L, -1)));
   /* removes 'value'; keeps 'key' for next iteration */
   lua_pop(L, 1);
}

, lua_next() , . lua_tolstring() , , , .

+10

Source: https://habr.com/ru/post/1710016/


All Articles