in reply to Back Navigation In A File
But when you seek to a certain position, Perl does not look at the bytes before the position you seek to (otherwise, seeking wouldn't be fast), so it has no way of knowing which line it is. Don't forget that on many systems, including Unix and Windows systems, files are just streams of bytes. The OS has no concept of lines.
If you must know the line numbers, what you can do is first you read the entire file, line-by-line, keeping track on which byte each line starts. You then have an index. Given the index, you can binary search to find the line number, given a position you seeked to.
Abigail
|
|---|