This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Birdgeek3's profile picture

While loop problems

Started by Birdgeek3, 30 December 2012 - 07:31 AM
Birdgeek3 #1
Posted 30 December 2012 - 08:31 AM
Hey guys,

So I am writing a script to go to a website read the data and print it a repeated numbert of times. The loop will occur for the amount of times the user enters. I am running into some issues with the While loop not stopping once the number of times repeated is equal to the number of times the user wants it to occur.

Here is the code

--Variables
local tArgs = { ... }
local num = (tArgs[1])
--Functions
function logPrint ()
local response = http.get( "http://guudelp.com/logsimple.cgi" )
local sResponse = response.readAll()
response.close()
print ( sResponse )
end
--Main Script
local i = 0
print (num)
sleep(3)
while i ~= num do
  logPrint()
  sleep(5)
  i = i + 1
  print (i)
  sleep (5)
end

Any ideas?
I have tried a repeat loop also but have no clue how to work those.
All the prints are debugging.

Pastebin link so you can test: http://pastebin.com/AVD4KFrr
Jack #2
Posted 30 December 2012 - 08:50 AM
Try a for loop. They're easy:

for i = 1,num do
  logPrint()
  sleep(5)
  print(i)
  sleep(5)
end
remiX #3
Posted 30 December 2012 - 09:26 AM
for loops are easier, but using a while loop should still work.

Try changing
local num = (tArgs[1])
to
local num = tonumber(tArgs[1]) 

As the arguments are returned as strings, so now you're comparing i which is a number with num which is a string. tonumber() will convert it to a number.
Birdgeek3 #4
Posted 30 December 2012 - 09:33 AM
for loops are easier, but using a while loop should still work.

Try changing
local num = (tArgs[1])
to
local num = tonumber(tArgs[1]) 

As the arguments are returned as strings, so now you're comparing i which is a number with num which is a string. tonumber() will convert it to a number.

This seemed to be the trick! Thank you :)/>