How large your file is affects the options you have. If the file is less than a few hundred kilobytes, it might be easiest to load the file into memory and treat it like an array.
If your file is larger, but static, you may want to build a series of indexes that represent the byte offset of the start of each line. If the file changes with any about of frequency, however, this option would probably not be so good.
The problem is that reading backwards in a file on a line by line basis is not a "natural" thing for most (if any) operating systems to do. Most operating systems support fairly efficient methods of moving of the file pointer around, but have no concept of "lines". Lines are an arbitrary construct, and are dependant on what you call your end of line character (typically a linefeed (unix), or carriage return/linefeed (DOS/Windows), or someother character of the user's choosing.
While there isn't any code in this example to tell you how to do it, maybe it'll give you some ideas about how to manage the data.
--Chris
Update: Or better yet, use
btrotts solution.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.