Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ Lua Insert multiple sub_tables into table with loop
Lua Insert multiple sub_tables into table with loop
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Gregh
(1 post) Bio
|
Date
| Thu 21 Sep 2023 05:20 AM (UTC) |
Message
| I want to insert multiple subtables into a parent table. Each sub table is the same format, just with different values based on the loop number. For some reason, the values for each sub table are all the same using the last loop.
Script:
local json = require('rapidjson')
local sub_tbl = {a = 1, b = 2, c = {a=1, b=2}}
local tbl = {widgets = {}}
for i=1, 4 do
sub_tbl.a = i*1
sub_tbl.b = i*2
sub_tbl.c.a = "id_"..tostring(sub_tbl.a)
sub_tbl.c.b = "id_"..tostring(sub_tbl.b)
table.insert(tbl,sub_tbl)
end
return json.encode(tbl)
And output:
[
[
{"b":8,"a":4,"c":{"b":"id_8","a":"id_4"}},
{"b":8,"a":4,"c":{"b":"id_8","a":"id_4"}},
{"b":8,"a":4,"c":{"b":"id_8","a":"id_4"}},
{"b":8,"a":4,"c":{"b":"id_8","a":"id_4"}}
]
] | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #1 on Thu 21 Sep 2023 01:09 PM (UTC) Amended on Thu 21 Sep 2023 10:41 PM (UTC) by Fiendish
|
Message
| You need to create a new local subtable each time inside the loop. Otherwise you're just adding references to the same single subtable every time.
You probably should read https://www.lua.org/pil/2.5.html but think about it like this...
The way Lua works, your sub_tbl is not the table. Your sub_tbl just points to the table that exists on its own behind the scenes. When you do sub_tbl.a = i*2, you're saying "adjust the value for key a on the behind-the-scenes table that sub_tbl is pointing to" which then modifies that original table. When you do table.insert(tbl, sub_tbl), you're inserting a reference to that original table as well. The reference gets copied but the table itself never gets copied, and you never make more than one, so all edits edit the original table, and all references point to the original table, so all of your entries get updated.
If it were me, I'd do something like this instead:
local tbl = {}
for i=1,4 do
local a_value = i*1
local b_value = i*2
table.insert(
tbl, {a=a_value, b=b_value, c={a="id_"..a_value, b="id_"..b_value}}
)
end
Note that I didn't use tostring, because Lua will let you concatenate numbers and strings together as long as the first element in the chain is a string.
In my version, every table.insert call is receiving a reference to a brand new subtable, not just a reference to the same table as before. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
2,191 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top