Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Self-Writing File creates infinite loop 1

Status
Not open for further replies.

Zenor

Programmer
Joined
Jul 10, 2004
Messages
1
Location
US
I'm fairly new to perl, and I was playing around with a self-writing file, which looks like this:

Code:
#!/usr/bin/perl -w
open Self, "myFile" ;
open DaFile, ">>myFile";
my $i = 0;

while (my $temp = <Self>) {
print DaFile $temp if ($i>4);
$i++;
}
$i = 0;
close Self;
open Self, "myFile" ;
#########11111111111##########

If it worked as I intended, it would append a copy of the file, excluding the first four lines, to the end of it. That is, after running it once, there would be two while-loops, instead of one.

Running it yet again should produce 6 while-loops: the two that were already there, and then two more from each of the two while loops.

This is exactly what happened. However, running it a third time, instead of producing 42 while-loops, created an infinite loop...by the time I had stopped it, I had a 300MB text file of while-loops!

Just out of curiosity, I manually created files with multiple while-loops of the above code. Five while-loops produced 30 while loops, as expected, but the manually created 6 while-loops also sent the program into an infinite loop.(As did any number greater than 6).

So anyway, I'm wondering if any of you have an idea what is going on here. I'm suspecting it has something to do with how the perl interpreter reads in files, but I don't know nearly enough about how it works to try to understand it.

Thanks for any help you can give.

 
Read more ...

-Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
you are reading from and writing to the same file
it must end in a infinite loop.
 
Insert:

select DaFile;$|=1;

right after you open DaFile, and I think you'll see your infinite loop on the first run, rather than third.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top