lua-users home
lua-l archive

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


2013/12/3 唐兆宁 <yuxiaichou@gmail.com>:
> My problem is: I want to push a table to lua stack,not by the index,but the
> key value,which means I want to push a C++ map to lua table.For example,if I
> have a map "a" -> 1, "b" -> 2,  can I get a lua table {a=1, b=2}? I find the
> lua_newtable and lua_rawseti could not work in my situation,Is there a
> solution? BTW, I use lua4.0.

I can only give a 5.2 answer but this a very basic question.

Say you have already got your new empty table on the stack in register t.
Then one field at a time, push the value on the stack and lua_setfield it.

E.g.

lua_newtable(L);  {* table is now in -1 *}
lua_pushinteger(L,1); {* table is now in -2 *}
lua_setfield(L,-2,"a");
lua_pushinteger(L,2); {* table is now in -2 *}
lua_setfield(L,-2,"b");