Create a table for LUA stack in C

来源:互联网 发布:用户画像数据分析工具 编辑:程序博客网 时间:2024/05/16 11:05

 static int getUsers(lua_State *L)
 {
     char *name[3]={"chen","Li","Huang"};

     char *sex[3]={"male","female","female"}

     int i;
     lua_newtable(L);
     for (i=0;i<3;i++)
     {
         lua_newtable(L);
         /*set child table*/
         {
             lua_pushstring(L, "name");
             lua_pushstring(L, name[i]);
             lua_settable(L,-3);
             lua_pushstring(L, "sex");
             lua_pushstring(L, sex[i]);
             lua_settable(L,-3);
         }
         /*set child table key*/
          lua_rawseti(L, -2, i+1);
     }
     return 1;
 }

 

LUA SCRIPT

local users=getUsers()

users[1].name,users[1].sex