### Author Topic: (Solved) For loop embedding question  (Read 5114 times)

0 Members and 1 Guest are viewing this topic.

#### zutokaza

• Full Member
• Posts: 146
• Active - Making stories can take the most time.
##### (Solved) For loop embedding question
« on: November 10, 2016, 08:21:59 AM »
Is it possible to do this in a shorter way?
Code: ( lua) [Select]
`for rep = 1, 15 do--blah different coordinate calculations for rep = 15, 30 do    --blah different different coordinates calculations  for rep = 30,45 do   --blah different coordinates calculations   for rep = etc....... do    --blah different coordinates calculations   end  end endend`
My first thought was to use arrays. Will this work? If yes, how would I have starter begin 0 and ender begin at 15?

Code: ( lua) [Select]
`for 1,15 do`
it would end up being:
15,30
30,45
45,60
etc

Code: ( lua) [Select]
`local starter={}local ender ={}for rep 1,15 do starter[rep] = 0 ender[rep] = 0endfor reprep = 1,15 do starter[reprep]=starter[reprep] +15 ending[reprep] = starter[reprep] +15 for rep = starter[reprep], ending[reprep] do --blah different coordinates calculations arrays endend`
« Last Edit: November 11, 2016, 02:44:26 AM by zutokaza »

#### Diarandor

• Hero Member
• Posts: 1062
• Cats are cool! （ΦωΦ）
##### Re: for loop question
« Reply #1 on: November 10, 2016, 12:02:00 PM »
Is it possible to do this in a shorter way?
Code: ( lua) [Select]
`for rep = 1, 15 do--blah different coordinate calculations for rep = 15, 30 do    --blah different different coordinates calculations  for rep = 30,45 do   --blah different coordinates calculations   for rep = etc....... do    --blah different coordinates calculations   end  end endend`

You probably noticed this: this part of code is completely wrong in many ways. You should avoid using the same variable name ("rep", in this case) for nested variables, since the new variable is not the same one: although the syntax is correct, the nested variable that has the same name ("rep") is a different variable that is hiding the other variable ("rep") of the outer scope. Besides, your inner loop will be executed 15 x 15 = 225 times aproximately instead of once, which is not what you want; so just put one loop after another and not one nested inside the other. I guess that your second and third loops should start with index 16 and 31 instead of 15 and 30.

My first thought was to use arrays. Will this work? If yes, how would I have starter begin 0 and ender begin at 15?

Yes, you can do that. Note that loops can start and end by any numeric value. You can also add an optional step (which can be negative if you want). Check some Lua tutorial: https://www.lua.org/pil/4.3.4.html
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

#### MetalZelda

• Hero Member
• Posts: 551
##### Re: for loop question
« Reply #2 on: November 10, 2016, 12:54:30 PM »
You can do shorter

Code: (lua) [Select]
`for i = 1, whatever_you_want do  if i < 15 then   -- calculation  elseif i < 30 then    -- other calculation  elseif i < 45   -- Again  endend`
This is recommanded to do so if you plan to do exponential coordination, for instance, admit that i[0; 15] have a fixed position of x + 15 where x = 10 (example)
simple way is

if i <= 15 then
table:draw(surface, 10 + (15 * i))
elseif i <= 30 then
-- Other position
table:draw(surface, 24 + (13 * i))
end

And it does the job

In the case of your example

Code: (lua) [Select]
`local starter={}local ender ={}for i = 1, 15 do  -- First, check if i = 1, if yes, then the start value would be 0  local start = i == 1 and 0 or starter[i]  -- starter and ender share the same result apparently ?  starter[i] = start + 15  ender[i] = starter[i]end`
Now let's assume you want to increment the starter and ender array in a different way, but still exponentially

Code: (lua) [Select]
`local starter={}local ender ={}for i = 1, 30 do  -- First, check if i = 1, if yes, then the start value would be 0  local start = i == 1 and 0 or starter[i]  local increment   if i <= 15 then    increment = 15  elseif i <= 30 then    increment = -30  end  -- starter and ender share the same result apparently ?  starter[i] = start + increment  ender[i] = starter[i]end`

Not tested but that should work
« Last Edit: November 10, 2016, 04:07:51 PM by MetalZelda »

#### zutokaza

• Full Member
• Posts: 146
• Active - Making stories can take the most time.
##### Re: for loop question
« Reply #3 on: November 11, 2016, 02:41:59 AM »
You can do shorter

Code: (lua) [Select]
`for i = 1, whatever_you_want do  if i < 15 then   -- calculation  elseif i < 30 then    -- other calculation  elseif i < 45   -- Again  endend`

This works. Thank you MetalZelda.