Recursively extracting information from a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ulexes
Prole
Posts: 3
Joined: Mon Jul 25, 2022 6:04 pm

Recursively extracting information from a table

Post by Ulexes »

Hello everyone!

I have a general logic problem that I have been unable to wrap my head around, and I am wondering whether any of you brilliant people might have ideas for possible solutions.

Let's say I have a table of straightforward numerical values that reflect the widths of individual columns, like so:

Code: Select all

cWidth = {c1, c2, c3}
I now intend to use these to plot the locations of successive vertical line for a printed chart. This would mean:
  • The location of the first line is cWidth[1]
  • The location of the second line is cWidth[1] + cWidth[2]
  • The location of the third line is cWidth[1] + cWidth[2] + cWidth[3]
My question is: How would you recommend I construct a for loop that moves through the table, collecting the values as needed?

My brain has only made it as far as this, before stalling out when I try some "i-1" stuff:

Code: Select all

-- Please assume "verticalLineTop" and "verticalLineBottom" are the top and bottom of the chart
-- "left" is the left border of the chart (i.e., the starting point for the measurements)
for i=1,cWidth in ipairs(cWidth) do
   local xPoint = (ALCHEMY INVOLVING i THAT I AM UNABLE TO DETERMINE)
   love.graphics.line(left + xPoint, verticalLineTop, left + xPoint, verticalLineBottom)
 end
Any advice is appreciated! Thanks for reading.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Recursively extracting information from a table

Post by ReFreezed »

Define a variable outside the loop to keep track of the sum of widths.

Code: Select all

local xPoint = left
for i, w in ipairs(cWidth) do
	xPoint = xPoint + w
	love.graphics.line(xPoint, verticalLineTop, xPoint, verticalLineBottom)
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Ulexes
Prole
Posts: 3
Joined: Mon Jul 25, 2022 6:04 pm

Re: Recursively extracting information from a table

Post by Ulexes »

Ugh, of COURSE it's something that simple! Thank you for your help, ReFreezed. This is exactly what I needed to move forward with my project.
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Recursively extracting information from a table

Post by Gunroar:Cannon() »

Ulexes wrote: Mon Jul 25, 2022 6:24 pm Hello everyone!

I have a general logic problem that I have been unable to wrap my head around, and I am wondering whether any of you brilliant people might have ideas for possible solutions.

Let's say I have a table of straightforward numerical values that reflect the widths of individual columns, like so:

Code: Select all

cWidth = {c1, c2, c3}
I now intend to use these to plot the locations of successive vertical line for a printed chart. This would mean:
  • The location of the first line is cWidth[1]
  • The location of the second line is cWidth[1] + cWidth[2]
  • The location of the third line is cWidth[1] + cWidth[2] + cWidth[3]
My question is: How would you recommend I construct a for loop that moves through the table, collecting the values as needed?

My brain has only made it as far as this, before stalling out when I try some "i-1" stuff:

Code: Select all

-- Please assume "verticalLineTop" and "verticalLineBottom" are the top and bottom of the chart
-- "left" is the left border of the chart (i.e., the starting point for the measurements)
for i=1,cWidth in ipairs(cWidth) do
   local xPoint = (ALCHEMY INVOLVING i THAT I AM UNABLE TO DETERMINE)
   love.graphics.line(left + xPoint, verticalLineTop, left + xPoint, verticalLineBottom)
 end
.
Also, for that i Alchemy...

Code: Select all

for i = 1, #cWidth do --# gets length of table
    local xPoint = cWidth[i]
    ...
end

The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests