[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C API question - modifing table
- From: Michael Roth <mroth@...>
- Date: Wed, 11 Aug 2004 13:52:56 +0200
Dmitry Samersoff wrote:
lua_tostring remove value from stack. Isn't it ?
So I should just push a key back late.
No! lua_toxxx don't remove (pop) the value from the stack!
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.
My sample loop fills the stack with key value pairs because I thought
you would like to. If you don't need this, you could write the loop much
easier:
lua_pushnil(L);
while (lua_next(L, t))
{
lua_pushvalue(L, -2); /* make copy of key */
const char *key = lua_tostring(L, -1); /* note index! (copy) */
const char *val = lua_tostring(L, -2);
cerr << "Key: " << key << " Val:" << val << endl;
lua_pop(L, 2); /* pops value and copy of key */
}
Attachment:
signature.asc
Description: OpenPGP digital signature