|
Prev: The map function
Next: FAQ 1.1 What is Perl?
From: PerlFAQ Server on 19 Apr 2008 03:03 This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.3: How do I count the number of lines in a file? One fairly efficient way is to count newlines in the file. The following program uses a feature of tr///, as documented in perlop. If your text file doesn't end with a newline, then it's not really a proper text file, so this may report one fewer line than you expect. $lines = 0; open(FILE, $filename) or die "Can't open `$filename': $!"; while (sysread FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } close FILE; This assumes no funny games with newline translations. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
From: szr on 19 Apr 2008 03:26 PerlFAQ Server wrote: [...] > 5.3: How do I count the number of lines in a file? > > > One fairly efficient way is to count newlines in the file. The > following program uses a feature of tr///, as documented in > perlop. If your text file doesn't end with a newline, then it's ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Shouldn't this be "If the lines in your text file do not end with a newline", or something to that effect? A text file need not have a "\n" at the very end :) > not really a proper text file, so this may report one fewer line > than you expect. [...] -- szr
From: J�rgen Exner on 19 Apr 2008 06:08 "szr" <szrRE(a)szromanMO.comVE> wrote: >PerlFAQ Server wrote: >[...] >> 5.3: How do I count the number of lines in a file? >> >> >> One fairly efficient way is to count newlines in the file. The >> following program uses a feature of tr///, as documented in >> perlop. If your text file doesn't end with a newline, then it's > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > >Shouldn't this be "If the lines in your text file do not end with a >newline", or something to that effect? No. It is a different way of saying "If there is stuff after the last newline then that stuff doesn't constitute a proper line..." >A text file need not have a "\n" >at the very end :) > >> not really a proper text file, so this may report one fewer line >> than you expect. "... and therefore it is not be counted as a line." jue
From: szr on 19 Apr 2008 18:05 J�rgen Exner wrote: > "szr" <szrRE(a)szromanMO.comVE> wrote: >> PerlFAQ Server wrote: >> [...] >>> 5.3: How do I count the number of lines in a file? >>> >>> >>> One fairly efficient way is to count newlines in the file. The >>> following program uses a feature of tr///, as documented in >>> perlop. If your text file doesn't end with a newline, then it's >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> Shouldn't this be "If the lines in your text file do not end with a >> newline", or something to that effect? > > No. It is a different way of saying "If there is stuff after the last > newline then that stuff doesn't constitute a proper line..." I don't recall there being any sort of rule saying a text file's last character /has/ to be a line terminator, though I can see how it would seem better formed if it does. -- szr
From: brian d foy on 20 Apr 2008 16:07
In article <fudqbu021qf(a)news4.newsguy.com>, szr <szrRE(a)szromanMO.comVE> wrote: > J�rgen Exner wrote: > > No. It is a different way of saying "If there is stuff after the last > > newline then that stuff doesn't constitute a proper line..." > > I don't recall there being any sort of rule saying a text file's last > character /has/ to be a line terminator, though I can see how it would > seem better formed if it does. There's no rule for it, but a lot of things expect it. Just Google "no newline at end of file" to see for yourelf. |