lua-users home
lua-l archive

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


If I populate a table then read it back like this

lua_newtable(L);
  lua_pushstring(L, "this is a test");
  luaL_ref(L, -2);
  lua_len(L, 1);
  printf("lua table has length %f\n", lua_tonumber(L, -1));
  lua_pushnil(L); /* first key */
  while(lua_next(L, 1 ) != 0)
  {
    /* uses 'key' (at index -2) and 'value' (at index -1) */
    if(lua_type(L,-2) != LUA_TNUMBER) {
      printf("%s - %s\n", lua_tostring(L, -2), lua_typename(L, lua_type(L, -1)));
    } else {
      printf("%f - %s\n", luaL_checknumber(L, -2), lua_typename(L, lua_type(L, -1)));
    }

    /* removes 'value'; keeps 'key' for next iteration */
    lua_pop(L, 1);
  }

I get the following output

lua table has length 1.000000
1.000000 - string
3.000000 - number

In Lua 5.3 I get

lua table has length 1.000000
1.000000 - string

Is this a bug, or am I missing something?

Thanks,

Bill