lua-users home
lua-l archive

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



Am 11.12.2012 um 20:41 schrieb Sean Conner:

It was thus said that the Great Philipp Kraus once stated:
Hello,

I generate a table with

lua_newtable
lua_pushxxx()
lua_setfield(L, -2, "mykey")
lua_setglobal


This works at the moment, I try to create a table like
{   "key1" : { "subkey" : 123 }, "key2" : { "subkey" : 456 } }

but I don't know how to append some new elements like
{   "key1" : { "subkey" : 123 }, "key2" : { "subkey" : 456, "newkey" : 789} }

I get the first table put it on top of the stack, than I use getfield to
check if the field exists, if it is nil, I pop the nil value and run newtable,
but if the value with the key exists (it must be a table), than the table is on top,
but how can I add on a table a new key-value pair? So I would like to
overwrite an existing key or I would like to append a new one.
Must I put all element on the stack top?

in short I try to solve this:

if table exists
    push it on the stack
    if key exists
       push it on the stack and replace value
    else
       push a new key-value pair on the stack
       add it to table
   move table from stack and append data
else
    create table
    create key-value pair and put it into table

Can me anybody explain how the append works?

 Assuming the following:

t = { key1 = { subkey = 123 } , key2 = { subkey = 456 , newkey = 789 } }
my_append(t)

Then:

int my_append(lua_State *L)
{
 luaL_checktype(L,1,LUA_TTABLE); /* t -- t      */ /* make sure we only have a table */
 lua_getfield(L,-1,"key2"); /* t -- t key2 */ /* get key2 */
 if (lua_isnil(L,-1))
 {
   lua_pop(L,1); /* t nil -- t     */ /* pop nil from stack */
   lua_createtable(L,0,0); /* t     -- t t   */ /* create a table */
   lua_pushvalue(L,-1); /* t t   -- t t t */ /* dup it */
   lua_setfield(L,-3,"key2"); /* t t t -- t t   */ /* and set it as key2 in the passed in table */
 }
 else
   luaL_checktype(L,-1,LUA_TTABLE); /* t t -- t t */ /* make sure it's a table */

 lua_pushinteger(L,314); /* t t     -- t t 314 */ /* set anewkey to 314 */
 lua_setfield(L,-2,"anewerkey");       /* t t 314 -- t t     */

 lua_pop(L,1); /* t t -- t */
 return 1; /* return passed in table */
}

Untested code, but the above should set key2.anewerkey to 314 in the passed
in table (and create key2 if it doesn't exist).  The first comments is a
stack diagram (a concept from Forth)---the top of the stack is always on the
right, and the diagram shows the stack before and after the function
(stack-before-call '--' stack-after-call), which should make the logic a bit
more clearer.


Thanks for this great explantation, but I have got the problem, that my second value
will not pushed into the table.

I create first this table { subpack1 : 123 } and push this table to a global var "pack". This works,
but if I pushed the second value the value is always nil.

I read the global var first and it is a table value, and with this code I try to append
a new value

lua_getfield( p_state, lua_gettop(p_state), p_package[p_pos].c_str() );
bool l_tableexists = lua_istable(p_state, lua_gettop(p_state));

                

if (!l_tableexists)
{
   lua_pop( p_state, -1 );
   lua_newtable( p_state );
}

append( p_state, p_functions, p_package, p_pos+1 );           
lua_setfield( p_state, -2, p_package[p_pos].c_str() );

the "append" is a recursiv method, in package are stored the key names and pos the current
key name. p_state is the lua state pointer. After the first append shows the table

pack : { subpack1 : 123 }
on the second insert it should be pack : { subpack1 : 123, subpack2 : 123 } but pack.subpack2 is nil

Do you have got an idea to solve the problem?

Thanks

Phil