ElGuapo
(Starting to like KiXtart)
2009-08-04 10:11 PM
Endless Loop

I am looking at the open and readline functions on p. 53-54 of the KiXtart 2010 manual, and I don't understand why the code below is producing an endless loop. Mylist.txt contains only 1 line.
 Code:
 
$datafile="c:\mylist.txt"

If Open (1,"$datafile") = 0 
   $line = ReadLine(1)  
   While @error = 0
	$line = ReadLine(1)
	myfunction()
   Loop
EndIf
Exit

Function myfunction()
 ? "My Function"
EndFunction


Mart
(KiX Supporter)
2009-08-04 10:20 PM
Re: Endless Loop

You are calling MyFunction, this function will set @error to 0 if it succeeds in displaying the text so While @error = 0 will always be true. Some other action that returns something else then 0 will end the loop.

You might want to swap the Readline and MyFunction() that way you can also do stuff when it has read the first line.


ElGuapo
(Starting to like KiXtart)
2009-08-04 10:29 PM
Re: Endless Loop

Thank you. That helps.

ElGuapo
(Starting to like KiXtart)
2009-08-04 10:39 PM
Re: Endless Loop

Putting END at the end of my file is what I came up with, but I'm wondering if there is a more elegant way to do this?

 Code:
$datafile="c:\mylist.txt"

If Open (1,"$datafile") = 0 
   $line = ReadLine(1)  
   While $line <> "END"
	  	myfunction()
		$line = ReadLine(1)
   Loop
EndIf
Exit

Function myfunction()
 ? "ResetVariables"
EndFunction


Mart
(KiX Supporter)
2009-08-04 10:58 PM
Re: Endless Loop

A small example.

 Code:
$datafile = "c:\mylist.txt"

If Open(1, $datafile, 2) = 0 
	$line = ReadLine(1)  
	While Not @ERROR
		? "Reset variables"
		Sleep 2
		$line = ReadLine(1)
	Loop
EndIf


Mylist.txt:
 Quote:

Line1
Line2
Line3


Glenn BarnasAdministrator
(KiX Supporter)
2009-08-05 10:25 PM
Re: Endless Loop

To elaborate just a bit on Mart's suggestion, when you create a loop that's controlled by @ERROR being false, you MUST place the command or function that will set the error you are interested in LAST in the loop code. For example:
 Code:
$Line = ReadLine(2)
While Not @ERROR ; do this stuff until an error occurs
  ; stuff goes here to process $Line
  $Line = ReadLine(2) ; will set @ERROR when no more data is available (EOF Error)
Loop
Since you want to read the data until there's no more data to read, the ReadLine will return 0 when it has read data, and an error when it has no more data due to encountering the End Of File. Note the use of negative logic in "While NOT @ERROR" - loop as long as no error exists.

Glenn


ElGuapo
(Starting to like KiXtart)
2009-08-05 11:34 PM
Re: Endless Loop

Thank you again.