943,682 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 2211
  • Perl RSS
Mar 1st, 2009
0

Reading from a file

Expand Post »
Hi All,
I am trying a simple program in PERL, but unable to get it working

Perl Syntax (Toggle Plain Text)
  1. $log="test.log";
  2. open(LOGFILE, $log) or die("Could not open log file.");
  3. foreach $line (<LOGFILE>) {
  4. chomp($line);
  5. if($line eq "Raghu") {
  6. print "Found a group\n";
  7. $var=<LOGFILE>;
  8. chomp($var);
  9. print "$var\n";
  10. }
  11. }

Whenever the line contains the letters "Raghu", I am trying to read the next line into the var variable. But nothing is getting printed as output except the statement "Found a group".
What is wrong in the code?
Thanks a lot for the time.
The test.log is as follows

Raghu
Hai
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
raghavendra83 is offline Offline
12 posts
since Jan 2009
Mar 1st, 2009
1

Re: Reading from a file

The problem is, the method you use to read from the file handle, slurps the file, and then iterates over each line by replacing $line with $_. If you really want to prove this, you can throw the special variable $. into your code. $. is a special variable in perl that represents the "current line number" in the file. If you run this code:
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. $log="test.log";
  4. open(LOGFILE, $log) or die("Could not open log file.");
  5. foreach $line (<LOGFILE>) {
  6. chomp($line);
  7. if($line eq "Raghu") {
  8. print "Found a group\n";
  9. print "$.\n";
  10. $var = <LOGFILE> or die ("couldn't read: $!\n"); # Error Checking
  11. chomp($var);
  12. print "$var\n";
  13. }
  14. }
  15. close(LOGFILE); # // How 'bout you close your open file handles :p
Your output from this will be:
Perl Syntax (Toggle Plain Text)
  1. Found a group
  2. 10
  3. couldn't read:
  4.  
Naturally, your line number (ie: the 10 here) will probably be different, depending on the number of lines in your file. It is important to note, that the program (with my minor modifications) dies after it tries to read from the file handle a second time.... this is because perl has slurped the file, so we have already seeked to EOF (end of file). The 10 here, shows that the current line number (in my case 10) is already at the end of the file. If this were not the case, then we would see the line with 10 change from 1 to 2 to 3, etc.

A better method for iterating over each line of the file, is to use a while loop. This will prevent perl from slurping the file, and give you the desired functionality.
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. $log="test.log";
  4. open(LOGFILE, $log) or die("Could not open log file.");
  5. while ($line = <LOGFILE>) {
  6. chomp($line);
  7. if($line eq "Raghu") {
  8. print "Found a group\n";
  9. $var = <LOGFILE> or die ("couldn't read: $!\n"); # Error Checking
  10. chomp($var);
  11. print "$var\n";
  12. }
  13. }
  14. close(LOGFILE); # // How 'bout you close your open file handles :p
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 1st, 2009
0

Re: Reading from a file

ditto everything Comatose said.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Regular Expression for Date(dd/mm/yyyy format)
Next Thread in Perl Forum Timeline: specify the file address





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC