in reply to Looking for a better way to get the number of lines in a file...
Well, what's a line?
The first line starts by the beginning of the file and ends at the first LF (or CRLF depending upon your operating system).
Most of the lines will start just after the preceeding line termination and continue to the next line termination (LF or CRLF).
Finally, one line may continue until end of file. This is not really clean, since for a 'true text file', the last byte(s) in the file should be a line terminator.
All of the above for a file that contains at least four lines.
Now, the problem has become to count the number or line terminators in the file and potentially add one to that. ("Until end of file.")
To count occurences of something in a (non indexed) file you will have to read it from the beginning until the end.
You could read the file byte by byte and verify each byte if it's a CR or an LF. If it's a CR, will the next byte be LF? (Assuming DOS/Windows as an OS.)
Why not let the file read figure out what is a line terminator for you? Then read one line at a time, count it and then throw it away.
Using $. is fine if you make some assumptions. $. is reset when the file handle is closed. From perlvar: Because <> never does an explicit close, line numbers increase across ARGV files. Take care.
There are smarter ways of doing it. But then you'll have to start making assumptions on how a line ends. And that the last line actually is correctly terminated. (Well, it should. But...)
f--k the world!!!!
/dev/world has reached maximal mount count, check forced.
|
|---|