Solved

Perl File open and read in reverse

Posted on 2006-11-01
5
220 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
ID: 17850074
#!/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
ID: 17850551
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
ID: 17853906
  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
ID: 17857239
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
ID: 17861238
What did you try and how did it crash?
Do you have File::ReadBackwards installed?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

821 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