Solved

Perl File open and read in reverse

Posted on 2006-11-01
5
211 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:precision
5 Comments
 
LVL 17

Accepted Solution

by:
mjcoyne earned 500 total points
Comment Utility
#!/usr/bin/perl -w
use strict;

open (FILE1,"../gt/$in{'id'}/rafs.txt") or die "Can't open rafs.txt $!";

my @lines = <FILE1>;
@lines = reverse @lines;

for (@lines) {
  chomp;
  my($count,$wa,$id,$n1,$e1,$n2,$e2,$n3,$e3,$n4,$e4) = split/\|/;
}
0
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
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);
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
  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/\|/;
0
 

Author Comment

by:precision
Comment Utility
OZO

That seems like a very efficient way, I tried though and it crashed, I tried a few things but could not make run...
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
What did you try and how did it crash?
Do you have File::ReadBackwards installed?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now