Search code examples
cinterfaceluakey-valuekeyvaluepair

What is the C interface to Lua for accessing a table's key/value pairs?


In Lua, using the C interface, given a table, how do I iterate through the table's key/value pairs?

Also, if some table table members are added using arrays, do I need a separate loop to iterate through those as well or is there a single way to iterate though those members at the same time as the key/value pairs?


Solution

  • lua_next() is just the same as Lua's next() function, which is used by the pairs() function. It iterates all the members, both in the array part and the hash part.

    If you want the analogue of ipairs(), the lua_objlen() gives you the same functionality as #. Use it and lua_rawgeti() to numerically iterate over the array part.