Solved

Perl File open and read in reverse

Posted on 2006-11-01
5
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

628 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