0

The core detail of my problem is: I don't know how to do it.

I encountered the problem I'm trying to solve by wanting to add a table to an existing table in Lua.

The major difficulty that prevented me from solving it myself is ignorance, and a failure to come across any internet search results that are useful to me in solving the problem, despite my best efforts.

I tried the following, among other things:

  lua_getglobal(L, "OuterTable"); 
    lua_createtable(L, 0,0); 
    lua_pushliteral(L, "InnerTable");
    lua_settable(L,-2);

I expected that "InnerTable" would be added to "OuterTable" after considering the accepted answer of this question:

How to create table in table in Lua 5.1 using C-API?

Which also used lua_settable to add a table to a table, albeit a newly created one.

I have also looked through Programming In Lua, the color table example, but I was not able to solve it with that one, either.

Minimum reproducible example:

test.lua

OuterTable = {
    ExampleInnerTable = {}
}

Testbed in C (I use Lua 5.3 but my distro is set up in a peculiar way so ...I don't know how YOU would include it, be wary of that)

  #include <lua5.3/lua.h>
  #include <lua5.3/lualib.h>
  #include <lua5.3/lauxlib.h>

int main(int argc, char *argv[])
{
    


  lua_State* L;
  L = luaL_newstate();
  luaL_openlibs(L);
  luaL_dofile(L, "test.lua");

 lua_getglobal(L, "OuterTable"); 
 int c = lua_gettop(L);
 lua_newtable(L);
 lua_pushliteral(L, "InnerTable");
 lua_settable(L,c);

    return 0;
}

Basically, I want to add another table to OuterTable, one like ExampleInnerTable

Desired result

OuterTable = {
    ExampleInnerTable = {}
   ,InnerTable = {}
}
5
  • Could you please provide a minimal reproducible example, e.g. a C file I can feed my compiler which doesn't work if executed? Commented Sep 17, 2023 at 9:22
  • I have updated my question with that (and accidentally deleted the comment where I told you this already, while thinking that clicking "delete" on a new comment would cancel the creation of the comment. Thus...this duplicate. Sorry , lol. Commented Sep 17, 2023 at 9:42
  • You know, I think this actually works... I just failed to 're push' the initial global, outer table, onto the stack to inspect it ...since settable pops things off the thing. ...dang. I'm sorry for the confusion and inconvenience. Do I delete non questions like this? Or what happens to em? Commented Sep 17, 2023 at 10:04
  • Heh, the rubber ducky effect in action again :) Deletion should be fine. You could also answer it yourself if you believe your answer may be useful to others (but this doesn't seem to be the case). Otherwise it would probably be closed as "not reproducible or caused by a typo". Commented Sep 17, 2023 at 10:09
  • Yes, SO can be used as a rubber duck :-) Commented Sep 17, 2023 at 20:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.