lua-users home
lua-l archive

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


----- Original Message ----- From: "Eugen-Andrei Gavriloaie"


The table might have keys 1,2,3,55,34,22,"sdfds",etc. This table is returned by calling a lua function (is on the C++ stack). After doing some c++ processing I'd like to add more "anonymous" values (with no keys) to the table and pass it as a parameter to anoter lua function. So, I have no guarantee that the key is not in use. :(

Um, just so we're clear, the table: {1, 2, 3, 55, 34, 22}
does not have any "anonymous" values. It's merely a short hand way of writing:
{ [1] = 1, [2] = 2, [3] = 3, [4] = 55, [5] = 34, [6] = 22 }
As in Lua there is no such thing as a value in a table without a key. How would you retrieve such a value?

(Your wording was a little vague).

So to add more values, simply use the code provided earlier (lua_rawseti(L, -2, lua_objlen(L, -2) + 1)). See, t[#t+1] is always nil, by definition. So you're always safe to add more values there.

(If you mean that want you want a way of attaching additional info to a table without modifying the table in anyway, that's a separate problem that can be solved by weak tables).

- Alex