CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [cliffyiu] how to compare two simple txt files: Edit Log



7stud
Enthusiast

Mar 18, 2010, 2:55 PM


Views: 8204
Re: [cliffyiu] how to compare two simple txt files


Quote
I understand in one while loop, if I get to the last line of a file, I won't start over again. But what I don't understand is, these are two independent while loops and why the second while doesn't read DATA from the beginning although the first while already reaches the last line of DATA.

Ok, so after the first while loop reaches the end of the file, the while loop doesn't start over from the beginning of the file again, and the while loop ends. What causes the reads from a file to start at the beginning again? Does writing:


Code
while (<SOME_FILE_HANDLE>)


cause the reads to start at the beginning of the specified file? Let's test that out. Run this program:


Code
use strict; 
use warnings;
use 5.010;

while (<DATA>) {
print "first while(): $_";
last;
}

while (<DATA>) {
print "second while(): $_";
last;
}

while (<DATA>) {
print "third while(): $_";
last;
}

while (<DATA>) {
print "fourth while(): $_";
last;
}

__END__
10
20
30

Examine the output. How many lines of output are there? How many while() loops are there? Come up with a theory to explain what is happening.


(This post was edited by 7stud on Mar 18, 2010, 3:11 PM)


Edit Log:
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 2:56 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 2:57 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 2:58 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 2:58 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:02 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:03 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:05 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:05 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:06 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:10 PM
Post edited by 7stud (Enthusiast) on Mar 18, 2010, 3:11 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives