lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am presently trying to take a LUA table which is saved in a file and
load it and iterate over each of the nested tables and print out the
key/value pairs.  What I am having trouble understanding is how to
properly extract the key/value pairs.

Below is my code:

// start code
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "savedtable.lua");
lua_getglobal(L,"test");

if(!lua_istable(L,-1)) {
  printf("Unable to load LUA file, it isn't a table"); } else {
  lua_pushnil(L);
  while(lua_next(L,-2) != 0) {
    lua_pop(L,1);
  }
}

lua_close(L);
// end code

What I have is a sample file "savedtable.lua" which contains the following data:

test = {
  ["admin"] = {
    ["preferences"] = {
      {
         {
           "Some text value here",
           nil,
           1,
           4,
           [0] = "28517",
         },
         {
           "another text string",
           nil,
           1,
           3,
           [0] = "29242",
         },
      },
    },
  },
}

How would I go about reading this table?

Is it better to use the lua API inside C/C++ or would it be easier for
me to write a simple LUA script that I load and execute from the C/C++
framework and the LUA script itself loads the table file and parses
it?

Any help is greatly appreciated.

Chris