954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading from a file

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

$log="test.log";
open(LOGFILE, $log) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
    chomp($line); 
	if($line eq "Raghu") {
		print "Found a group\n";
		$var=<LOGFILE>;
		chomp($var);
		print "$var\n";
	}
}


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

raghavendra83
Newbie Poster
12 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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:

#!/usr/bin/perl

$log="test.log";
open(LOGFILE, $log) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
    chomp($line); 
	if($line eq "Raghu") {
		print "Found a group\n";
		print "$.\n";
		$var = <LOGFILE> or die ("couldn't read: $!\n"); # Error Checking
		chomp($var);
		print "$var\n";
	}
}
close(LOGFILE);  # // How 'bout you close your open file handles :p


Your output from this will be:

Found a group
10
couldn't read:


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.

#!/usr/bin/perl

$log="test.log";
open(LOGFILE, $log) or die("Could not open log file.");
while ($line = <LOGFILE>) {
    chomp($line); 
	if($line eq "Raghu") {
		print "Found a group\n";
		$var = <LOGFILE> or die ("couldn't read: $!\n"); # Error Checking
		chomp($var);
		print "$var\n";
	}
}
close(LOGFILE);  # // How 'bout you close your open file handles :p
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

ditto everything Comatose said.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You