Nowayz Nowayz - 1 year ago 103
C++ Question

Creating a Lua Table from a const char **

I have a

const char **
which is going to be of varying lengths, but I want to create a Lua array from the
const char **

const char **
is something like this


I need to convert this array to a global table in Lua, but I'm not sure about how to go about this as I'm not very good at manipulating Lua.

Answer Source
int main()
   char* arg[3] = {
      "Yellow" };

   //create lua state
   Lua_state* L = luaL_newstate();

   // create the table for arg
   int table_index = lua_gettop(L);

   for(int i =0; i<3; ++i )
      // get the string on Lua's stack so it can be used

      // this could be done with lua_settable, but that would require pushing the integer as well
      // the string we just push is removed from the stack
      // notice the index is i+1 as lua is ones based

   //now put that table we've been messing with into the globals
   //lua will remove the table from the stack leaving it empty once again
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download