lua-users home
lua-l archive

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

On Mon, Nov 02, 2009 at 03:54:20PM +0800, sagasw wrote:
>    I have a question when I read the manual.
>    lua_gettable could get table, and lua_getfield could get specific key.
>    If I only know the table in top of stack like:
>    local tbl_sample = {a = "aaa", b = "bbb", c= "ccc", 15}
>    How could I get the information of table key-value pairs in c? And the
>    case is I don't know the key.
>    Just like the function ipairs(), it doesn't need to know the key before
>    using the table.
>    I read the source code of ipairs(), it says to use upvalue to aux the
>    function.
>    Who could give me c code sample for it?

Just call lua_next(L, index_of_table). It's in the Reference Manual.

If stack starts like this:
    -1: nil
    -2: your table

then after lua_next(L, -2), the stack will look like this:
    -1: "aaa"
    -2: "a"
    -3: your table

The Ref Manual has sample code for table traversals.