• Subject: Re: Another table question...
• From: Eugen-Andrei Gavriloaie <shiretu@...>
• Date: Wed, 9 Apr 2008 23:39:47 +0300

```
On Apr 9, 2008, at 9:42 PM, Alex Davies wrote:

```
```----- 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.
```
Yes you are right. But here is an example of an unwanted value overwrite: 1. Lua calls a c++ with the following argument: {"first_anon_value","second_anon_value",false,22}. I know that internally this table is represented like this:
```	1=> first_anon_value
2=> second_anon_value
3=> false
4=> 22
```
1,2,3 and 4 key values are computed by lua internally (I really don't know the algorithm behind the scene)
```

```
2. the c++ function is making some computing and decide that row number 2 must be deleted. So, internal representation is now like this:
```	1=> first_anon_value
3=> false
4=> 22

```
3. We now try to add a new value using your method. Let this value be the string "this is a string". lua_objlen(L, -2) + 1 evaluates to 4 in this case.
```I think (not sure) that after lua_rawset the table will look like this:
1=> first_anon_value
3=> false
4=> this is a string
As you can see 22 was overwritten with "this is a string".

Extract from the docs:

--begin cut--
lua_rawseti

[-1, +0, m]

void lua_rawseti (lua_State *L, int index, int n);

```
Does the equivalent of t[n] = v, where t is the value at the given valid index and v is the value at the top of the stack.
```
```
This function pops the value from the stack. The assignment is raw; that is, it does not invoke metamethods.
```--end cut--

So the question is: n denotes a position or 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
```
```

```