1.一些用來遍歷lua表的api簡介
以下是對下面幾個函數的認識不對請批評指正:
lua_istable:是否是一個表
lua_gettable(L,int index) :把lua棧的索引為index表的lua棧的index+1所指的索引的值彈出。也就是彈出table[index+1];
lua_next(L,index):先把 表(lua棧 index所指的表), 的當前索引彈出,再把table 當前索引的值彈出,也就是先彈出 table的索引,再彈出table索引的值
2.代碼示例
// lua_table_extent.cpp : 定義控制台應用程序的入口點。 // #include "stdafx.h" #include "stdafx.h" #include "lua.hpp" #include "lauxlib.h" #include "lualib.h" #include <string.h> #include <windows.h> #include <iostream> using namespace std; #pragma comment(lib,"lua5.1.lib") #pragma comment(lib,"lua51.lib") /* luaJ_table.lua文件內容 ---------------------------------------------- NUMBER_TABLE = { 11, 22, 33, 44, } NUMBER_TABLE_WITH_INDEX = { ["a"] = 1, ["b"] = 2, ["c"] = 3 } STRING_TABLE_WITH_INDEX = { ["a"] = "this is a", ["b"] = "this is b", ["c"] = "this is c" } ----------------------------------------------- */ int _tmain(int argc, _TCHAR* argv[]) { lua_State *L = luaL_newstate(); luaL_openlibs(L); if(0 != luaL_loadfile(L,"lua_table.lua")) { printf("loadbuff error :%s",lua_tostring(L,-1)); lua_pop(L,1); } if(0 != lua_pcall(L,0,0,0)) { printf("pcall error :%s",lua_tostring(L,-1)); lua_pop(L,1); } lua_getglobal(L,"STRING_TABLE_WITH_INDEX"); /*此時lua棧狀態 ---------------------------------- | -1 table NUMBER_TABLE ---------------------------------- */ if(!lua_istable(L,-1)) cout<<"not a table"<<endl; /*此時lua棧狀態 ---------------------------------- | -1 table NUMBER_TABLE ---------------------------------- */ lua_pushnumber(L,1); /*此時lua棧狀態 ---------------------------------- | -1 1 key | -2 table NUMBER_TABLE ---------------------------------- */ lua_gettable(L,-2); /*此時lua棧狀態 ---------------------------------- | -1 1 Value | -2 table NUMBER_TABLE ---------------------------------- */ if(lua_isnumber(L,-1)) cout<<lua_tonumber(L,-1)<<endl; else if(lua_isstring(L,-1)) cout<<lua_tostring(L,-1)<<endl; /*此時lua棧狀態 ---------------------------------- | -1 1 Value | -2 table NUMBER_TABLE ---------------------------------- */ lua_pop(L,1); /*此時lua棧狀態 ---------------------------------- | -1 table NUMBER_TABLE ---------------------------------- */ //循環遍歷 lua_pushnil(L); /*此時lua棧狀態 ---------------------------------- | -1 nil | -2 table NUMBER_TABLE ---------------------------------- */ while(lua_next(L,-2)) { /*此時lua棧狀態 ---------------------------------- | -1 value | -2 key | -3 table NUMBER_TABLE ---------------------------------- */ if(lua_isnumber(L,-2)) cout<<"key:"<<lua_tonumber(L,-2)<<'\t'; else if(lua_isstring(L,-2)) cout<<"key:"<<lua_tostring(L,-2)<<'\t'; if(lua_isnumber(L,-1)) cout<<"value:"<<lua_tonumber(L,-1)<<endl; else if(lua_isstring(L,-1)) cout<<"value:"<<lua_tostring(L,-1)<<endl; /*此時lua棧狀態 ---------------------------------- | -1 value | -2 key | -3 table NUMBER_TABLE ---------------------------------- */ lua_pop(L,1); /*此時lua棧狀態 ---------------------------------- | -1 key | -2 table NUMBER_TABLE ---------------------------------- */ } lua_pop(L,1); /*此時lua棧狀態 ---------------------------------- | -1 table NUMBER_TABLE ---------------------------------- */ lua_close(L); system("pause"); return 0; }
以上代碼的輸出為
key:a value:this is a key:c value:this is c key:b value:this is b 請按任意鍵繼續. . .