lua-users home
lua-l archive

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


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?

Thanks

Phil