Reading from a file

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 12
Reputation: raghavendra83 is an unknown quantity at this point 
Solved Threads: 0
raghavendra83 raghavendra83 is offline Offline
Newbie Poster

Reading from a file

 
0
  #1
Mar 1st, 2009
Hi All,
I am trying a simple program in PERL, but unable to get it working

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Reading from a file

 
1
  #2
Mar 1st, 2009
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:
  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:
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Reading from a file

 
0
  #3
Mar 1st, 2009
ditto everything Comatose said.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC