read file reverse order...

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
bionicblakeyAsked:
Who is Participating?
 
PerlKingConnect With a Mentor Commented:
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
 
rj2Commented:
open(FILE,"data.txt") || die("Can not open file because $!");
@lines = reverse <FILE>;
close(FILE);
print @lines;
0
 
FishMongerConnect With a Mentor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
bionicblakeyAuthor Commented:
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
 
PerlKingCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.