lua-users home
lua-l archive

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

On Apr 9, 2008, at 6:04 PM, Jerome Vuarand wrote:

Jerome Vuarand wrote:
Eugen-Andrei Gavriloaie wrote:
Here is a piece of lua script:

a={some_key="some value...",some_other_key=233.2,"value without key"}


Normally I do:

lua_push... //key
lua_push... /value
lua_settable(L, -3);

But lua_settable is extracting 2 elements from the stack. What should
I put instead of the first lua_push to obtain an auto assigned key?

You can't get the exact same result without tracking the number of
"auto-assigned" keys yourself, but you can use the following to have
a similar result:

lua_push...(L, value);
lua_rawseti(L, -2, lua_objlen(L, -2));

There is an obvious mistake in the above, correct code is:

lua_push...(L, value);
lua_rawseti(L, -2, lua_objlen(L, -2) + 1); /* < note the +1 */
Tx for the response. But is some kind dangerous because the table might already contain a row with the same key. Am I right?