[Resolved] adding a "sub-key" to a table

I am sure this is a basic question but I can’t seem to get this right…
I want to copy data into an existing table that hasn’t got that sub-key. In this example I though this was as easy as if I just added the .name key to friendsWithApp[k]
But I get the error attempt to index field ‘?’ (a string value)

--given this two tables  
local friends ={1,32 }  
local dataCopy ={{name="hello"},{name="world"} }  
--create a new one and copy the content  
local friendsWithApp={}  
friendsWithApp = friends  
  
--try to add the .name key and assign the value  
for k, v in pairs (friendsWithApp) do  
 friendsWithApp[k].name = dataCopy[1].name -- end  

Thanks for the help, I don’t know if this is possible or would like to know about alternative ideas [import]uid: 74667 topic_id: 30337 reply_id: 330337[/import]

The err you got is quite normal.
You can’t add a field ‘name’ if friendsWithApp[k] is not a table.

Actually, friendsWithApp[k] refers to a number.
friendsWithApp[1] = 1
friendsWithApp[2] = 32.

Though, I’m not sure to understand what you want as an output. Can you be a little bit more precise ? [import]uid: 142361 topic_id: 30337 reply_id: 121539[/import]

Thanks for the explaining that for me, the output I was looking for is to get the friendsWithApp like this:

friendsWithApp={{id=1,name="hello"},{id=32,name="world"}}

I think figured it out, thanks a ton!! :slight_smile:

[code]

local friends ={1,32 }
local dataCopy ={{name=“hello”},{name=“world”} }

local friendsWithApp={}
for k, v in pairs (friends) do
table.insert(friendsWithApp,{id=friends[k],name=dataCopy[k].name})
end
[/code] [import]uid: 74667 topic_id: 30337 reply_id: 121555[/import]

That’s good.
Anyway, I want to point out two things.

Say that you have a table t:

t = { x = 1, y = 2}  
t2 = t  

Do not ever think that t2 is a copy of table t. That is totally false.
Truth it, table t2 is a reference, as t is, to a table in memory. Or say ‘pointers’, if that helps to figure out.
Just try this, to be sure.

t = { x = 1, y = 2}  
t2 = t  
print(t) --\> table: 0x231b9d0  
print(t2)--\> table: 0x231b9d0  

t and t2 have both the same memory address. So they both refers to the same object.
That’s a common mistake for those who begins with Lua.
Thefrefore, if you need to need a real copy of a table, you will have to loop through to copy all its contents.

function raw\_copy(t)  
 local ret = {}  
 for k,v in pairs(t) do  
 if type(v) == 'table' then ret[k] = raw\_copy(v)  
 else ret[k] = v  
 end  
 end  
 return ret  
end  

Second point. I don’t know what purpose you want t store such informations in tables. But, my advise is, if this collection of informations is about to grow, then you might want to consider another way to store data. Cause actually, it’s not that efficient.

friendsWithApp={{id=1,name="hello"},{id=32,name="world"}}

What you need here is a pair of values : ID, then name. Whay not simply make something like:

friendsWithApp={[1] = "hello", [32] = "world"}

Then, to loop through, you will need pairs() instead of ipairs(), as keys here are not consecutive.
That will save a lot of memory, though. But I suggest this only if you do not need to associate any other properties to each ID.
[import]uid: 142361 topic_id: 30337 reply_id: 121558[/import]

thanks again for taking the time to help, this is really valuable information for me. I will make the changes in my code. cheers! :slight_smile: [import]uid: 74667 topic_id: 30337 reply_id: 121561[/import]

The err you got is quite normal.
You can’t add a field ‘name’ if friendsWithApp[k] is not a table.

Actually, friendsWithApp[k] refers to a number.
friendsWithApp[1] = 1
friendsWithApp[2] = 32.

Though, I’m not sure to understand what you want as an output. Can you be a little bit more precise ? [import]uid: 142361 topic_id: 30337 reply_id: 121539[/import]

Thanks for the explaining that for me, the output I was looking for is to get the friendsWithApp like this:

friendsWithApp={{id=1,name="hello"},{id=32,name="world"}}

I think figured it out, thanks a ton!! :slight_smile:

[code]

local friends ={1,32 }
local dataCopy ={{name=“hello”},{name=“world”} }

local friendsWithApp={}
for k, v in pairs (friends) do
table.insert(friendsWithApp,{id=friends[k],name=dataCopy[k].name})
end
[/code] [import]uid: 74667 topic_id: 30337 reply_id: 121555[/import]

That’s good.
Anyway, I want to point out two things.

Say that you have a table t:

t = { x = 1, y = 2}  
t2 = t  

Do not ever think that t2 is a copy of table t. That is totally false.
Truth it, table t2 is a reference, as t is, to a table in memory. Or say ‘pointers’, if that helps to figure out.
Just try this, to be sure.

t = { x = 1, y = 2}  
t2 = t  
print(t) --\> table: 0x231b9d0  
print(t2)--\> table: 0x231b9d0  

t and t2 have both the same memory address. So they both refers to the same object.
That’s a common mistake for those who begins with Lua.
Thefrefore, if you need to need a real copy of a table, you will have to loop through to copy all its contents.

function raw\_copy(t)  
 local ret = {}  
 for k,v in pairs(t) do  
 if type(v) == 'table' then ret[k] = raw\_copy(v)  
 else ret[k] = v  
 end  
 end  
 return ret  
end  

Second point. I don’t know what purpose you want t store such informations in tables. But, my advise is, if this collection of informations is about to grow, then you might want to consider another way to store data. Cause actually, it’s not that efficient.

friendsWithApp={{id=1,name="hello"},{id=32,name="world"}}

What you need here is a pair of values : ID, then name. Whay not simply make something like:

friendsWithApp={[1] = "hello", [32] = "world"}

Then, to loop through, you will need pairs() instead of ipairs(), as keys here are not consecutive.
That will save a lot of memory, though. But I suggest this only if you do not need to associate any other properties to each ID.
[import]uid: 142361 topic_id: 30337 reply_id: 121558[/import]

thanks again for taking the time to help, this is really valuable information for me. I will make the changes in my code. cheers! :slight_smile: [import]uid: 74667 topic_id: 30337 reply_id: 121561[/import]