Solved

read file reverse order...

Posted on 2004-04-14
6
1,246 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 23
Perl strange behaviour 5 73
stftime format 4 56
Need Helping Editing Perl Script for PowerChute Network Shutdown 3 84
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 …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

809 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