c++ - Looping through tables in a table in Lua -


i've hit complete dead end this. going incredibly basic , result in me smashing head wall having major brain fart. question basically, how loop though tables in lua if entries tables themselves?

c++:

lua_newtable(luastate);     for(auto rec : recpay) {         lua_newtable(luastate);          lua_pushnumber(luastate, rec.amount);         lua_setfield(luastate, -2, "amount");          lua_pushnumber(luastate, rec.units);         lua_setfield(luastate, -2, "units");          lua_setfield(luastate, -2, rec.type);     } lua_setglobal(luastate, "recuringpayments"); 

lua:

for _,recwt in ipairs(recuringpayments)     -- recwt.amount = nil? end 

in c++ code looks you're setting subtable string key rather index. traverse entry have use pairs instead:

for rectype, recwt in pairs(recuringpayments)   assert(recwt.amount ~= nil) end 

note ipairs traverses index part of table, associative part ignored.

alternatively, if want use index access have set key-value lua_settable instead:

lua_newtable(luastate); int = 0; for(auto rec : recpay) {     lua_newtable(luastate);      lua_pushnumber(luastate, rec.amount);     lua_setfield(luastate, -2, "amount");      lua_pushnumber(luastate, rec.units);     lua_setfield(luastate, -2, "units");      lua_pushnumber(luastate, ++i);     lua_insert(luastate, -2);     lua_settable(luastate, -3); } lua_setglobal(luastate, "recuringpayments"); 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -