LUA_NEXT(3) Library Functions Manual LUA_NEXT(3)

lua_nextpops a key from the stack, and pushes a key-value pair from the table, function indicator [-1, + (2|0), e]

#include <lua.h>

int
lua_next(lua_State *L, int index);

() pops a key from the stack, and pushes a key-value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then lua_next returns 0 (and pushes nothing).

A typical traversal looks like this:

/* 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 - %s0,
         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);
}

While traversing a table, do not call lua_tolstring(3) directly on a key, unless you know that the key is actually a string. Recall that lua_tolstring(3) changes the value at the given index; this confuses the next call to lua_next.

lua_tolstring(3)

Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, Lua 5.1 Reference Manual.

The lua_next() manual page is based on Lua Reference Manual 5.1 and was created by Sergey Bronnikov.

July 20, 2022 Debian