Solved

read file reverse order...

Posted on 2004-04-14
6
1,269 Views
Last Modified: 2012-05-04
i have a file and like to read it in a reverse order..

for example if my file has
1
2
3
4
5

and i like to read in order 5, 4, 3, 2, 1....

is there a way i can do that??

or is there anyway that i can append a file at the top instead at bottom.

for example, if i want to append 6,
i like my file to have
6
1
2
3
4
5
0
Comment
Question by:bionicblakey
[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
6 Comments
 
LVL 10

Expert Comment

by:rj2
ID: 10826790
open(FILE,"data.txt") || die("Can not open file because $!");
@lines = reverse <FILE>;
close(FILE);
print @lines;
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 250 total points
ID: 10826968
You can use Tie::File which will:

DESCRIPTION
Tie::File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on.

The file is not loaded into memory, so this will work even for gigantic files.

Changes to the array are reflected in the file immediately.

http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm

There also is a module (which I'll look for) that will allow you to open and read the file line-by-line starting from the end.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10826988
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
PerlKing earned 250 total points
ID: 10831379
Although you can achieve this the way FishMonger or rj2 have suggested and in a lot of other ways, but all of them ultimately have to look for newlines and seperate out the lines. This is a tremendous waste of effort at the low level.

If this is only a one-time or ad-hoc requirement working with a small amount of data it is fine, but if you are upto something serious, then I would strongly suggest against this idea. You could probably consider revising your design so that you may avoid the need to deal with your files this way.
0
 

Author Comment

by:bionicblakey
ID: 10838521
i agree w/ PerlKing.  i have to use this alot...

is there anyway, that i can append my file on the top instead of at the bottom??
0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10839425
The Tie::File module that FishMonger suggested, provides an "unshift" operator. So, you can use it like this:

use Tie::File;
tie @array, 'Tie::File', "myfile.txt" or die "Cannot tie file";

my $new_rec = "This is a new record";
unshift @array, $new_rec;

This will prepend the line "This is a new record" at the top of the file instead of at the end.

But here again, I have to point out that at the low level, what the module has to be doing is "something like" the following:
1. Rememeber the existing contents of the file (in memory or on hard drive)
2. First write the new record to a new file (or the same file reopened)
3. Then write the remembered contents of the file after the new record
This again is very very costly.

To put it simply, "reading a file from beginning to end" and "appending things at the end of the file" are concieved to be "natural" things to do. Hence filesystems are designed to work best for these operations. Any other "un-natural" operation would turn out to be costly and will speak on the performance of the system if used extensively.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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

728 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