lua-users home
lua-l archive

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


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