简体   繁体   中英

How to create a multi-dimensional Lua table with Lua C Api...?

Hi I want lua C api to create Lua table like

table={['key1']={5,4,3,2},['key2']={1,0,1,1,0},['key3']={0,10,0,30,0,50}}

thanks in-advance....

My lua2c gives this:

lua_newtable(L);
lua_pushliteral(L,"key1");
lua_newtable(L);
lua_pushnumber(L,5);
lua_pushnumber(L,4);
lua_pushnumber(L,3);
lua_pushnumber(L,2);
lua_rawseti(L,-5,4);
lua_rawseti(L,-4,3);
lua_rawseti(L,-3,2);
lua_rawseti(L,-2,1);
lua_pushliteral(L,"key2");
lua_newtable(L);
lua_pushnumber(L,1);
lua_pushnumber(L,0);
lua_pushnumber(L,1);
lua_pushnumber(L,1);
lua_pushnumber(L,0);
lua_rawseti(L,-6,5);
lua_rawseti(L,-5,4);
lua_rawseti(L,-4,3);
lua_rawseti(L,-3,2);
lua_rawseti(L,-2,1);
lua_pushliteral(L,"key3");
lua_newtable(L);
lua_pushnumber(L,0);
lua_pushnumber(L,10);
lua_pushnumber(L,0);
lua_pushnumber(L,30);
lua_pushnumber(L,0);
lua_pushnumber(L,50);
lua_rawseti(L,-7,6);
lua_rawseti(L,-6,5);
lua_rawseti(L,-5,4);
lua_rawseti(L,-4,3);
lua_rawseti(L,-3,2);
lua_rawseti(L,-2,1);
lua_settable(L,-7);
lua_settable(L,-5);
lua_settable(L,-3);
lua_setglobal(L,"table");

This automatically generated code postpones setting table entries to the end; it can be more readable to set them as soon as possible, but you need to adjust the indices carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM