Consider the following lua table:
foo = {
bar1 = {a = 1, b = 2, c = "hello"},
bar2 = {a = 5, b = 2, c = "bbq"},
bar3 = {a = 76, b = 13, c = "pwnd"}
}
I am trying to iterate this table using the lua C API to get the key names, bar1, bar2 and bar3. I used the function lua_next(L, -2)to iterate, as many have suggested, but the problem is that it returns elements in random order. The order changes with each run.
I am using the following code:
for( lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1) )
{
printf("%s\n", lua_tostring(L, -2));
}
In most cases, the output is unordered, for example bar2 bar1 bar3. When lucky, he ordered. Is there an easy way to encode table keys in an orderly way? What will be the equivalent code that I use but ordered? Thank!
Edit:
, , . lua script ipairs, . API C. qaru.site/questions/681510/..., , , , .