LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with perl (https://www.linuxquestions.org/questions/programming-9/help-with-perl-518649/)

kshkid 01-12-2007 07:57 AM

help with perl
 
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";
}


jim mcnamara 01-12-2007 09:04 AM

seek <handle>, <offset>, <whence>
where whence is
0=beginning
1=current pos
2=eof


All times are GMT -5. The time now is 03:24 AM.