table question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

table question

Lee Smith-2
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.

Reply | Threaded
Open this post in threaded view
|

Re: table question

Romulo Bahiense
I don't know if this is what you are trying to do, but...

Replace:

  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");

with:

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


I have not tested, but might work.

--rb

Reply | Threaded
Open this post in threaded view
|

Re: table question

Lee Smith-2
Romulo Bahiense wrote:

I don't know if this is what you are trying to do, but...

Replace:

  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");

with:

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


I have not tested, but might work.

--rb

That seems to work, until I try to create another table at index 2:

(append)

 tableIndex++;
 lua_pushnumber(L, tableIndex);
 lua_newtable(L); // push 1

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

 lua_pushstring(L, "item4"); // push 1
 lua_pushstring(L, "value (item 4)"); // push 1
 lua_settable(L, -3); // pop 2

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "3rdtable"); // pop 1


Reply | Threaded
Open this post in threaded view
|

Re: table question

Romulo Bahiense
Lee Smith wrote:
That seems to work, until I try to create another table at index 2:

(append)

 tableIndex++;
 lua_pushnumber(L, tableIndex);
 lua_newtable(L); // push 1

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

 lua_pushstring(L, "item4"); // push 1
 lua_pushstring(L, "value (item 4)"); // push 1
 lua_settable(L, -3); // pop 2

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "3rdtable"); // pop 1

Just move the instruction << lua_setglobal(L, "testtable"); >> to the end, becoming:

  lua_pushvalue(L, -1); // push 1
  lua_setglobal(L, "3rdtable"); // pop 1
  lua_setglobal(L, "testtable");

Don't forget to _move_ it, not copy.

--rb

Reply | Threaded
Open this post in threaded view
|

Re: table question

Lee Smith-2
This fails:

 lua_newtable(L); // push 1

 lua_pushnumber(L,tableIndex); // push 1
 lua_newtable(L); // push 1

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

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

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "2ndtable"); // pop 1

 tableIndex++;
 lua_pushnumber(L, tableIndex);
 lua_newtable(L); // push 1

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

 lua_pushstring(L, "item4"); // push 1
 lua_pushstring(L, "value (item 4)"); // push 1
 lua_settable(L, -3); // pop 2

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "3rdtable"); // pop 1

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


When being accessed like so:

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

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

  teststring = testtable[2]["item3"]
  print("teststring[item3]: " .. teststring )

  teststring = testtable[2]["item4"]
  print("teststring[item4]: " .. teststring)


Reply | Threaded
Open this post in threaded view
|

Re: table question

Romulo Bahiense
Lee Smith wrote:
This fails:

 lua_newtable(L); // push 1

 lua_pushnumber(L,tableIndex); // push 1
 lua_newtable(L); // push 1

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

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

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "2ndtable"); // pop 1

You are missing a << lua_settable(L, -3) >> here.

 tableIndex++;
 lua_pushnumber(L, tableIndex);
 lua_newtable(L); // push 1

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

 lua_pushstring(L, "item4"); // push 1
 lua_pushstring(L, "value (item 4)"); // push 1
 lua_settable(L, -3); // pop 2

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "3rdtable"); // pop 1

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



Try to replace with this:

  /* ... */

  lua_pushvalue(L, -1); // push 1
  lua_setglobal(L, "2ndtable"); // pop 1

  lua_settable(L, -3);

  tableIndex++;
  lua_pushnumber(L, tableIndex);
  lua_newtable(L); // push 1

  /* ... */

--rb

Reply | Threaded
Open this post in threaded view
|

Re: table question

Lee Smith-2
Romulo Bahiense wrote:

Lee Smith wrote:

This fails:

 lua_newtable(L); // push 1

 lua_pushnumber(L,tableIndex); // push 1
 lua_newtable(L); // push 1

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

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

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "2ndtable"); // pop 1


You are missing a << lua_settable(L, -3) >> here.

 tableIndex++;
 lua_pushnumber(L, tableIndex);
 lua_newtable(L); // push 1

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

 lua_pushstring(L, "item4"); // push 1
 lua_pushstring(L, "value (item 4)"); // push 1
 lua_settable(L, -3); // pop 2

 lua_pushvalue(L, -1); // push 1
 lua_setglobal(L, "3rdtable"); // pop 1

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




Try to replace with this:

  /* ... */

  lua_pushvalue(L, -1); // push 1
  lua_setglobal(L, "2ndtable"); // pop 1

  lua_settable(L, -3);

  tableIndex++;
  lua_pushnumber(L, tableIndex);
  lua_newtable(L); // push 1

  /* ... */

--rb

Awesome, that worked splendidly. Thanks! Now I'm going to write some wrapper functions to hide all this from myself so I don't screw it up all over again :).