Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: read a file in both directions

by herby1620 (Monk)
on May 31, 2006 at 17:10 UTC ( [id://552871]=note: print w/replies, xml ) Need Help??


in reply to read a file in both directions

Well, let's see. You read the 9 track tape in the forward direction till you get a record of EBCDIC blanks, then skip to the end of file and do a read backwards (the operation does exist!) on the tape back to the original record. :-) :-)

Sorry, that was with record oriented files. Now days most file systems are continuous streams of characters, and the record boundaries are defined by context. The normal operation to read from a file does so in the forward direction and advances the pointer by that same amount. This is the problem. if you desire to read a file in the reverse direction, you must do so with forward semanticts[sp?]. This means you must "backup" a record, read the record just backed over, then "backup" again. It isn't easy, but it can be done. Others have indicated there is a Perl module to help you in this, and if done correctly ("tie" to a file handle comes to mind) it might even seem like a normal operation.

That being said, reading in the reverse direction isn't commonly done, as there are usually better ways to do the same thing. Reading the records into a hash, and operating on them that way is probably a "better idea". Also consider the original dataset. It might be better formatted at the source to eliminate the need to do the fancy dance with the data. A good look at "why" you need to access the data in this manner might pay off better in the long run.

Good luck!

Replies are listed 'Best First'.
Re^2: read a file in both directions
by dsheroh (Monsignor) on May 31, 2006 at 22:40 UTC
    Reading the records into a hash, and operating on them that way is probably a "better idea".

    Don't you mean "Reading the records into an array..."? I assume the questioner needs to preserve the order of the records (if only so they can be accurately reversed) and hashes generally destroy that order.

    A good look at "why" you need to access the data in this manner might pay off better in the long run.

    Indeed!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://552871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found