Page 1 of 1

Recursively extracting information from a table

Posted: Mon Jul 25, 2022 6:24 pm
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.

Re: Recursively extracting information from a table

Posted: Mon Jul 25, 2022 9:17 pm
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

Re: Recursively extracting information from a table

Posted: Mon Jul 25, 2022 9:39 pm
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.

Re: Recursively extracting information from a table

Posted: Fri Jul 29, 2022 8:32 am
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