lua-users home
lua-l archive

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


Using the C API, is there a more elegant way of creating a 2d table with strings as the keys than this? I really hate the idea of having to use lua_setglobal, only to immediately follow it with a getglobal as it seems unneccessary, however I cannot seem to figure out another way of doing it.

 unsigned int tableIndex = 1;

 lua_newtable(L);
 lua_pushnumber(L,tableIndex);

 lua_newtable(L); // push
 lua_pushstring(L, "item1"); // push
 lua_pushstring(L, "value (item 2)"); // push
 lua_settable(L, -3); // pop 2

 lua_pushstring(L, "item2"); // push
 lua_pushstring(L, "value (item 2)"); // push
 lua_settable(L, -3); // pop 2
 lua_setglobal(L, "2ndtable"); // pop 1
 lua_getglobal(L, "2ndtable"); // push 1

 lua_settable(L, -3); // push 2
 lua_setglobal(L, "testtable");

I've tried using lua_rawseti with this call:
lua_rawseti(L, -3, 2);

Instead of using set/get global, but I get seg faults at that point.

I'd like to access the tables like so:

  print "printing c++ created table contents"
  teststring = testtable[1]["item1"]
  print("teststring[item1]: " .. teststring )

  teststring = testtable[1]["item2"]
  print("teststring[item2]: " .. teststring)


Thanks for any input.