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?
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.
lua_next()
next()
pairs()
ipairs(), lua_objlen() , #. lua_rawgeti() .
ipairs()
lua_objlen()
#
lua_rawgeti()
, 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); }
/* 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() , , , .
lua_tolstring()
Source: https://habr.com/ru/post/1710016/More articles:Blt () to create a layer effect. Does not work. Am I using the wrong boolean function or something else? - c ++How to disable "opt for upgrade" in Oracle using Perl DBI - oracleWPF - IsEnabled DependencyProperty binding not working properly - buttonCreate a registration form so that IE remembers the login information - htmlHow do you structure the database for a wiki site? - wikiMake a sentence from a grammar with a given number of terminals - language-agnosticHow can I get text from a component in a JList? - javaGeneric SortedList, Как найти индекс первого элемента, который больше, чем ключ поиска? - genericsHow can I read and write to a binary file at the same time? - fileHow many programming languages do you use at one time? - programming-languages | fooobar.comAll Articles