Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl File open and read in reverse

Posted on 2006-11-01
5
Medium Priority
?
239 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

715 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