Solved

read file reverse order...

Posted on 2004-04-14
6
1,226 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

18 Experts available now in Live!

Get 1:1 Help Now