lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


we use floats in Lua, but there is a problem, which, probably, is
because of this change (we still use Lua 5.02). When iterating via a
table, which indices are numbers, runtime error occured:

"invalid key for 'next'"

Therefore we iterate via Lua tables in the following way (we presume
that table to iterate is on the top of the stack):

// standard cycle
        lua_pushnil     (state);
        for ( ; lua_next(state,-2); )
            lua_pop     (state,1);
// modified cycle
        lua_pushnil     (state);
        for ( ; lua_next(state,-2); ) {
            // start of the magic change
            if (lua_isnumber(state,-2)) {
                lua_Number      next = lua_tonumber(state,-2);
                lua_pop         (state,2);
                lua_pushnumber  (state,next);
            // end of the magic change
            lua_pop      (state,1);

Best regards,