C/C++访问Lua中table元素方法总结

方式一:

访问key为数字的table

extern "C"{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
};
#pragma comment(lib,"lua5.1.lib")


int main()
{
 lua_State* L = luaL_newstate();
   if (luaL_loadstring(L,"back= { 0.30, 0.10, 0 }")
         || lua_pcall(L,0,0,0)) {
        printf("Error Msg is %s.\n",lua_tostring(L,-1));
       return -1;
    }
//    if (luaL_dofile(L,"test.lua")) {
//     printf("Error Msg is %s.\n",lua_tostring(L,-1));
//     return -1;
//     }
   lua_getglobal(L,"back");
   if (!lua_istable(L,-1)) {
    printf("'background' is not a table.\n" );
    return -1;
    }
   int size=lua_objlen(L,-1);
  for (int i=1;i<=size;i++)
  {
     lua_pushnumber(L,i);
     lua_gettable(L,-2);
     printf("%f\n",lua_tonumber(L,-1));
  lua_pop(L,1);
  }
  
   

    lua_close(L);
    return 0;

}

 

 

 方式二:

访问key为字符串等任意的table

 

extern "C"{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
};
#pragma comment(lib,"lua5.1.lib")


int main()
{
 lua_State* L = luaL_newstate();
   if (luaL_loadstring(L,"back= { r=0.30, g=0.10, b=0 }")
         || lua_pcall(L,0,0,0)) {
        printf("Error Msg is %s.\n",lua_tostring(L,-1));
       return -1;
    }
//    if (luaL_dofile(L,"test.lua")) {
//     printf("Error Msg is %s.\n",lua_tostring(L,-1));
//     return -1;
//     }
   lua_getglobal(L,"back");
   if (!lua_istable(L,-1)) {
    printf("'background' is not a table.\n" );
    return -1;
    }
//   int size=lua_objlen(L,-1);
//    for (int i=1;i<=size;i++)
//    {
//     lua_pushnumber(L,i);
//     lua_gettable(L,-2);
//     printf("%f\n",lua_tonumber(L,-1));
//     lua_pop(L,1);
//    }

   lua_pushnil(L);
   while (lua_next(L,-2))
   {  
    printf("%f\n",lua_tonumber(L,-1));
    lua_pop(L,1);
   }
   

    lua_close(L);
    return 0;

}

 

方式三:http://blog.csdn.net/huangjm_13/article/details/8194201

C/C++听过key(键)或index(索引)来访问和修改Lua中的Table的内容

WriteLuaTable.lua

luat_Test1={a=123, b=456, c=789}
luat_Test2={123, 456, 789}

WriteLuaTable.cpp

#include <lua.hpp>

static void WriteTableFromKey(lua_State *L, const char* lpszTableName, const char* lpszTableItem, int nVal)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, nVal);
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}

static void WriteTableFromIndex(lua_State *L, const char* lpszTableName, int index, int nVal)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, nVal);
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}

static void ReadTableFromKey(lua_State *L, const char* lpszTableName, const char* lpszTableItem)
{
lua_getglobal(L, lpszTableName);

lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
printf("%s.%s=%d\n", lpszTableName, lpszTableItem, (int)lua_tonumber(L, -1));
lua_pop(L, 2);
}

static void ReadTableFromIndex(lua_State *L, const char* lpszTableName, int index)
{
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
printf("%s[%d]=%d\n", lpszTableName, index, (int)lua_tonumber(L, -1));
lua_pop(L, 2);
}

int main (int argc, char* argv[])
{
lua_State *L = lua_open();
luaopen_base(L);

luaL_dofile(L, "WriteLuaTable.lua");

ReadTableFromKey(L, "luat_Test1", "a");
ReadTableFromKey(L, "luat_Test1", "b");
ReadTableFromKey(L, "luat_Test1", "c");
puts("\n");
WriteTableFromKey(L, "luat_Test1", "a", 147); // luat_Test1['a'] = 147
WriteTableFromKey(L, "luat_Test1", "b", 258); // luat_Test1['b'] = 258
WriteTableFromKey(L, "luat_Test1", "c", 369); // luat_Test1['c'] = 369
WriteTableFromKey(L, "luat_Test1", "d", 159); // luat_Test1['d'] = 159
ReadTableFromKey(L, "luat_Test1", "a");
ReadTableFromKey(L, "luat_Test1", "b");
ReadTableFromKey(L, "luat_Test1", "c");
ReadTableFromKey(L, "luat_Test1", "d");
puts("\n--------------------------");
ReadTableFromIndex(L, "luat_Test2", 1);
ReadTableFromIndex(L, "luat_Test2", 2);
ReadTableFromIndex(L, "luat_Test2", 3);
puts("\n");
WriteTableFromIndex(L, "luat_Test2", 1, 147); // luat_Test2[1] = 147
WriteTableFromIndex(L, "luat_Test2", 2, 258); // luat_Test2[2] = 258
WriteTableFromIndex(L, "luat_Test2", 3, 369); // luat_Test2[3] = 369
WriteTableFromIndex(L, "luat_Test2", 4, 159); // luat_Test2[4] = 159
ReadTableFromIndex(L, "luat_Test2", 1);
ReadTableFromIndex(L, "luat_Test2", 2);
ReadTableFromIndex(L, "luat_Test2", 3);
ReadTableFromIndex(L, "luat_Test2", 4);
lua_close(L);
return 0;
}

你可能感兴趣的:(C/C++访问Lua中table元素方法总结)