[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page

Posted by Gregh   (1 post)  [Biography] bio
Date Thu 21 Sep 2023 05:20 AM (UTC)
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.


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)


return json.encode(tbl)

And output:


[Go to top] top

Posted by Fiendish   USA  (2,525 posts)  [Biography] 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

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
    tbl, {a=a_value, b=b_value, c={a="id_"..a_value, b="id_"..b_value}}

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.

[Go to top] 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.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]