It is currently August 5th, 2020, 1:24 pm

[Solved]Repeat Loop? (Lua)

Discuss the use of Lua in Script measures.
User avatar
revolt4peace
Posts: 83
Joined: February 3rd, 2015, 4:36 pm

[Solved]Repeat Loop? (Lua)

Post by revolt4peace »

So far I have:

Code: Select all

   repeat
      Check[i] = Coefficients[i]
      Numbers[1] = Numbers[1]..Check[i]
      i = i+1
   until (tonumber(Check[i]) >= 10 or tonumber(Check[i]) < 0) or Check[i] == 'x'
But for some reason, these few lines create an error. What am I doing wrong?

Here's the rest of the (important) code:

Code: Select all

function Initialize()
   Equation = SKIN:GetVariable('Equation')
   Solveable = 1
   Coefficients = {}
   Numbers = {}
   Check = {}
   k=0
   for i=1,#Equation do
      Coefficients[i] = '0'
   end
   for i=1,#Equation do
      Coefficients[i] = string.sub(Equation, i, i)
   end
   for i=1,#Equation do
      Numbers[i] = ''
   end
   for i=1,#Equation do
      Check[i] = '0'
   end	
   i=1
   repeat
      Check[i] = Coefficients[i]
      Numbers[1] = Numbers[1]..Check[i]
      i = i+1
   until (tonumber(Check[i]) >= 10 or tonumber(Check[i]) < 0) or Check[i] == 'x'
Any help is much appreciated!
Last edited by revolt4peace on February 17th, 2015, 7:16 pm, edited 1 time in total.
-__--__-- Explodingcreeperssss --__--__-
Click Me! - My Algebra Solver for Rainmeter!
User avatar
revolt4peace
Posts: 83
Joined: February 3rd, 2015, 4:36 pm

Re: Repeat Loop? (Lua)

Post by revolt4peace »

I have figured out that it has something to do with the last line:

Code: Select all

until tonumber(Check[i]) >= 10 or tonumber(Check[i]) < 0 or Check[i] == 'x'
and that it may have something to do with the possibility that Check may not be a string (or is it char?) that I thought it was. Possibly. Another possibility is that I'm doing something else wrong in that line...

Please help!
-__--__-- Explodingcreeperssss --__--__-
Click Me! - My Algebra Solver for Rainmeter!
User avatar
balala
Rainmeter Sage
Posts: 11156
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Repeat Loop? (Lua)

Post by balala »

In your original repeat .. until cycle, on the until check, Check is always equal with '0', because when you have certain value for i (and Check gets the value of Coefficients) after the assignement of Check and Numbers[1], the value of i increments, so the until condition is checked with this new value. So, on the until line Check is always equal with '0', the condition won't never be true and that's why the cycle never ends.
To avoid it, I'd move the assigning of Check array outside of the repeat .. until cycle:

Code: Select all

for i=1,#Equation do
	Check[i] = Coefficients[i]
end