<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > in front of the LUA advanced to read the two-dimensional table with C + +, but recently there is a new demand, LUA file inside the three-dimensional table, his own groping a bit, finally found a solution to the way, in here with you to share the </span>
The three-dimensional table structure in my LUA file is as follows
stage = {{m_ NId = 105,m_nboss = 0,m_nscorelist = {300,450,600},m_nrewardlist = {230401201,13020801,13010411},m_ndropitemlist = { 0,0,0}},{m_nid = 106,m_nboss = 0,m_nscorelist = {630,840,1050},m_nrewardlist = {241801101,14000201,18000201},m_ Ndropitemlist = {14000101,241700301,230501301}},{m_nid = 107,m_nboss = 0,m_nscorelist = {735,980,1225},m_nRewardList = { 230400201,230400411,230500101},m_ndropitemlist = {12020101,13010301,230401401}},{m_nid = 108,m_nBoss = 0,m_ Nscorelist = {840,1120,1400},m_nrewardlist = {19000101,241500901,18040201},m_ndropitemlist = { 14000401,230401201,14000101}},{m_nid = 109,m_nboss = 0,m_nscorelist = {1080,1440,1800},m_nrewardlist = { 12020301,230600101,241800501},m_ndropitemlist = {16000401,230901101,230700301}},{m_nid = 110,m_nBoss = 1,m_ Nscorelist = {1200,1600,2000},m_nrewardlist = {0,0,220200901},m_ndropitemlist = {230902101,231101101,231000401}}
};
Read the code below
#include <stdio.h>extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h"} #include <iostream> #include <vector> #include "string.h" using namespace Std;int main (int argc, char *argv[]) {lua_state* L; L=lua_open (); Luaopen_base (L); Luaopen_table (L); Luaopen_string (L); Luaopen_math (L); if (Lual_dofile (L, "Stage.lua")) {printf ("Run script failed\n");} else {int it_idx; int t_idx; Lua_getglobal (L, "stage"); T_idx=lua_gettop (L); cout << t_idx << Endl; Lua_pushnil (L); while (Lua_next (L,T_IDX)) {it_idx=lua_gettop (L); Lua_pushnil (L); <span style= "White-space:pre" ></span>//cout << "it_idx =" << it_idx << Endl int m_id = 0; while (Lua_next (L,IT_IDX)) {Const char* strkey = lua_tostring (l,-2); if (strcmp (strkey, "m_nid") = = 0) {m_id = (int) lua_tonumber (L,-1); cout << "m_id =" <<m_id << ""; } if (strcmp (strkey, "m_nboss") = = 0) {m_id = (inT) Lua_tonumber (L,-1); cout << "M_nboss =" << m_id << ""; } if (strcmp (strkey, "m_nscorelist") = = 0) {if (Lua_istable (L,-1)) {cout << "m_nscorelist ="; int ll_idx = Lua_gettop (L); cout << "ll_idx =" << ll_idx << Endl; Lua_pushnil (L); while (Lua_next (L, Ll_idx)) {cout << (int) Lua_tonumber (l,-1) << ""; Lua_pop (L, 1); }}} if (strcmp (strkey, "m_nrewardlist") = = 0) {if (Lua_istable (L,-1)) {cout << "m_nrewardlist = " ; int ll_idx = Lua_gettop (L); cout << "ll_idx =" << ll_idx << Endl; Lua_pushnil (L); while (Lua_next (L, Ll_idx)) {cout << (int) Lua_tonumber (l,-1) << ""; Lua_pop (L, 1); }}} if (strcmp (strkey, "m_ndropitemlist") = = 0) {if (Lua_istable (L,-1)) {cout << M_ndropitemlis t = "; int ll_idx = Lua_gettop (L); cout << "ll_idx =" << ll_idx << Endl; Lua_pushnil (L); WhiLe (Lua_next (L, Ll_idx)) {cout << (int) Lua_tonumber (l,-1) << ""; Lua_pop (L, 1); }}} lua_pop (l,1); } cout << Endl; Lua_pop (l,1); Break }} lua_close (L); return 0;}
Lua Advanced 8--C + + read three-dimensional tables in Lua files