Link to home
Start Free TrialLog in
Avatar of precision
precision

asked on

Perl File open and read in reverse

Here is what I have...

open (FILE1,"../gt/$in{'id'}/rafs.txt") || "Can't open filename1 $!";
while( <FILE1> ){
  chomp;
  my($count,$wa,$id,$n1,$e1,$n2,$e2,$n3,$e3,$n4,$e4)=split/\|/;

How can I make it open and read from bottom up?  I know reverse command, but not sure where to stick it in at.
ASKER CERTIFIED SOLUTION
Avatar of mjcoyne
mjcoyne

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will work for small files that fits into available memory. Otherwise smarter code is needed. The simplest way is to use existing sollutions

open(F, "tac 'filename' |") || die;
while(<F>)
{
 print($_);
}
close(F);
Avatar of ozo
  use File::ReadBackwards ;

    tie *FILE1, 'File::ReadBackwards', "../gt/$in{'id'}/rafs.txt" or die "can't read '../gt/$in{'id'}/rafs.txt' $!" ;

    while( <FILE1> ) {
       chomp;
       my($count,$wa,$id,$n1,$e1,$n2,$e2,$n3,$e3,$n4,$e4)=split/\|/;
Avatar of precision
precision

ASKER

OZO

That seems like a very efficient way, I tried though and it crashed, I tried a few things but could not make run...
What did you try and how did it crash?
Do you have File::ReadBackwards installed?