Hi All,
In the following piece of script, am retrieving the corresponding value based on the line no from another file.
Is there any way to reiterate the implicit file pointer in perl just like fseek without having to close and open the file each time.
Many thanks for your help!
Code:
#! /opt/third-party/bin/perl
my $badFile = "bad.txt";
my $goodFile = "good.txt";
my $outputFile = "out.txt";
my $globalLineNo = 0;
my ($badNum, $part, $line, $result);
open(BAD, $badFile);
open(GOOD, $goodFile);
open(OUT, "> $outputFile");
while ( chomp($badLine = <BAD>) )
{
my @split_fields = split(/,/, $badLine);
$badNum = @split_fields[0];
$part = @split_fields[1];
my @split_fields = split(/-/, $part);
$line = @split_fields[1];
$result = subFunction($line);
print "$badNum,$result\n";
print OUT "$badNum,$result\n";
}
close(BAD);
close(GOOD);
close(OUT);
exit(0);
sub subFunction
{
my $lno = shift;
if ( $lno < $globalLineNo ) {
close(GOOD);
open(GOOD, $goodFile);
$globalLineNo = 0;
}
while ( chomp($goodLine = <GOOD>) )
{
$globalLineNo++;
if ( $globalLineNo == $lno ) {
return $goodLine;
}
}
return "Not Found";
}