lua-users home
lua-l archive

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


From: Dirk Laurie
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.

I don't think Lua 4.0 had lua_setfield.

Probably something like (untested):

lua_newtable(L);
lua_pushstring(L, "a");
lua_pushnumber(L, 1);
lua_rawset(L, -3);
lua_pushstring(L, "b");
lua_pushnumber(L, 2);
lua_rawset(L, -3);

- Claire.