r/lua • u/Weekly_Flounder_1880 • Jun 05 '26
Help There have to be a simpler way to do this
I am a beginner and I have been very slowly learning Lua.
So, the problem here was to calculate the sum of all the pairs (separated by space). I swear there have to be a faster way to do this than making a for loop for every pair TT
Edit: yeh I still have a long long way to go, thank you everyone :)
Edit2: thanks whoever who repost this to r/programminghorror lol
77
Upvotes
27
u/mmknightx Jun 05 '26
Why are you having A1 to A13 as independent variables?
2
u/HyperrHydra Jun 05 '26
But why are you even doing this?
29
u/Saptarshi_12345 Jun 05 '26
I do not mean to offend you but I had a stroke reading this code I'm so sorry. The other folks here have given credible solutions and it is refreshing to see people actually programming and discussing rather than directly going to an AI agent. Way to go!
12
u/revereddesecration Jun 05 '26
You can put a for loop inside another for loop.
It’s useful to think about the “dimensionality” of your data. In this case, you have 13 rows and 2 columns - it’s two-dimensional data, like a spreadsheet.
For each column, you use a nested for loop. So for two dimensions, use a for loop inside a for loop.
9
u/fast-as-a-shark Jun 05 '26
This code activate a feeling inside me I haven't felt in a long time... Some sort of nostalgia...
3
u/nomenclature2357 Jun 05 '26
not clear at all what you are doing but you could put all your 'Anum' tables in a table and use one copy of the loop you have nested inside a loop that goes through the table of tables
3
u/xThomas Jun 05 '26 edited Jun 05 '26
here's a method using all the numbers in the same arrtable, as if you were just writing an array where every second number was paired with the previous numb. in this case, you can use an increment of 2, and go from 1 (the first index in the table) to 26 (the last index in the table) using the table length operator
#(though note that #tbl is unpredictable if you have a hole in the table). source: https://www.lua.org/manual/5.1/manual.html#2.5.5note: this code will not work as written, i forgot that insert is not a standard method on tables, the fix would be to use a metatable to make table.insert available and use the `:` (colon) operator instead of the `.` here. I'll leave the original code here, so that fixing it is left as an exercise to the reader
It's been a while since i wrote lua code but essentially it does the following:
loop 1: i=1, sums[1] = A[1] + A[2]
loop 2: i=3, sums[2] = A[3] + A[4]
notice that i increases by 2. so we should have 13 loops here, which means we end up creating 13 sums. probably. i didn't test, let me know how it goes.
3
u/No-Flight-573 Jun 05 '26
Others have rightly reminded you of nested tables, but for this specific task I'd prefer something really simple like the following (as I'm a fan of keeping the data as flat as possible, since I work mostly on high-perf software):
Short and sweet – nothing much to it!
3
u/no_brains101 Jun 05 '26 edited Jun 05 '26
Edit: ah u/mmknightx already beat me to this here
Also for the record, this way I gave is maybe bad too, you probably don't want to nuke your input list for your result, so if you dont want to, make a new one and table.insert into it or something
So,
Theres like, a lot of ways to do this, but like, this is probably the easiest next step from what you had
Also your A1..A13 and sum are all GLOBAL variables btw you should avoid that generally, use local otherwise you are gonna use the name A1 or sum somewhere else and be very confused.
3
u/jamescodesthings Jun 05 '26
I thought this post was on r/ProgrammingHorror when i saw it.
If I wanted ot hack through this I would just reference
_G["A.."var_number]in a for loop that sets var_number between 1, 13 or whatever.But ideal fix is to fix your data structure first(table of tables or whatever, then implement.
2
u/nadmaximus Jun 05 '26
To this day I can remember the moment I realized arrays could have multiple dimensions. I was coding BASIC on a TRS-80 Model II.
It was an understanding that increased my powers suddenly and dramatically. And made me thirsty for more. "there has to be a better way" has been a consistent thing ever since.
2
u/Spacedestructor Jun 05 '26
others already said this but my first thought was "why variables and not a list?" and lists have this beautiful thing where you can write generic code to process an arbitrary number of values without having to expect them.
Plus if you really need all of these loops you can nest them.
Every time you think "there must be a better way" with 99% there is going to be and your just not thinking of it, even when your experienced eventually this will keep happening.
2
u/Cootshk Jun 05 '26
local i = 1
while _G[“A”..i] do
…
end
2
u/realmauer01 Jun 06 '26
Just do a second for loop that just counts up
2
u/1984balls Jun 06 '26 edited Jun 06 '26
This one is probably a bit slow but yk
1
u/Outside-Drawing-4128 Sep 25 '26
Man faz uma planilha no deep seek sobre isso e usa ele como professor de programação, é bom pra quem tá começando
-2
u/csmit195 Jun 05 '26
this has to be rage bait
16
u/[deleted] Jun 05 '26
[removed] — view removed comment