Lua Insert multiple sub_tables into table with loop

Posted by Gregh on Thu 21 Sep 2023 05:20 AM — 2 posts, 7,113 views.

#0
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"}}

]
]
USA Global Moderator #1
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.