using namespace std;
#include "lua.hpp"
lua_State *L;
int getfield(lua_State *L,int key1,int key2)
{
int result = 0;
lua_pushnumber(L,key1);
lua_gettable(L,-2); //把t[k] 值壓入堆棧,這裏的 t 是指有效索引 index 指向的值,
//而 k 則是棧頂放的值。這個函數會彈出堆棧上的 key,把結果放在棧上相同位置。
lua_pushnumber(L,key2);
lua_gettable(L,-2);
result = (int)lua_tonumber(L,-1);
lua_pop(L,2);
return result;
}
int main()
{
L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"2.lua");
lua_getglobal(L,"t");
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 3; j++)
{
cout<<"result:"<<getfield(L,i,j)<<endl;
}
cout<<endl;
system("pause");
lua_close(L);
return 0;
}
/****************************2.lua**************************/
t ={ {11,12,13},{21,22,23} }