As Javier says, you want the lua_next()
function. I thought a code sample might help make things clearer since this can be a little tricky to use at first glance.
Quoting from the manual:
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 - %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);
}
Be aware that lua_next()
is very sensitive to the key value left on the stack. Do not call lua_tolstring()
on the key unless it really is already a string because that function will replace the value it converts.