lua-users home
lua-l archive

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


Michael Roth wrote:

> | I can add new value to table before iterate it,
> | but code below doesn't work where is my mistake ?
>
> There are three possible problems:
> a.) Maybe, your stack index 't' is a relative index. This won't work
> with your loop. Make sure, 't' is absolute.


t is absolute position, and could be safely replaced by 1 in this sample.

> |          /* `key' is at index -2 and `value' at index -1 */
> |         const char *key = lua_tostring(L, -2);
> |         lua_pushstring(L,key);
>

lua_tostring remove value from stack. Isn't it ?
So I should just push a key back late.

> ~        lua_checkstack(L, 2);
> ~        lua_pushvalue(L, -2);


It of course more accurate solution than re-push key by lua_pushstring.

> The above code is untested. Maybe it contains errors and bugs. ;-)


Your code works, but unfortunately problem still exists.
I can add my key to table before  I iterated throuph it, but not after it.

i.e
 lua_pushstring(L, "RRN");
 lua_pushstring(L, "abcd");
 lua_settable(L,-3);

works fine before loop but doesn't work after it.


--
Dmitry Samersoff
dms@samersoff.net, http://devnull.wplus.net
ICQ: 3161705
* There will come soft rains ...