lua-users home
lua-l archive

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


On Tuesday 09, Anselmo Junior wrote:
> But its return me the length of the table right? But dont return me if the
> 2 is nill and key 3 has some value...

Oops, sorry in your case it will not help.  I was thinking about the array 
part length.  It would work if all the numeric keys you wanted didn't have 
any gaps (i.e. 1,2,3,4,5 instead of 3,11,12)

I think you might be able to use the "next" method from LuaState.

>From the Lua 5.1 reference manual for "lua_next":

A typical traversal looks like this:

     /* 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);
     }

See the full description here:
http://www.lua.org/manual/5.1/manual.html#lua_next

But this will return all key/value pairs even ones with string keys, so check 
the key type.

>
> But i try this method to see what happen, and return me 0
>
>         state.getGlobal("init");
>         state.call(0, 1);
>         System.out.println(state.objLen(-1));
>
> when i use call method, the function 'init' will run and the return value
> is pushed on the stack. If i understand right, i can access this value with
> -1 index, where is the last element that be push on the stack.

yes this is right.  The -1 index is the top of the stack, and -2 would be the 
value below it.  Index 1 is the first value you pushed onto the stack.

-- 
Robert G. Jakabosky