I need to create Perl code which allows counting paragraphs in text files. I tried this and doesn’t work:
open(READFILE, "<$filename")
or die "could not open file \"$filename\":$!";
$paragraphs = 0;
my($c);
while($c = getc(READFILE))
{
if($C ne"\n")
{
$paragraphs++;
}
}
close(READFILE);
print("Paragraphs: $paragraphs\n");
If you’re determining paragraphs by a double-newline (“\n\n”) then this will do it:
Otherwise, just change the “\n\n” in the code to use your own paragraph separator. It may even be a good idea to use the pattern
\n{2,}, just in case someone went crazy on the enter key.If you are worried about memory consumption, then you may want to do something like this (sorry for the hard-to-read code):
Although, if you want to keep using your own code, you can change
if($C ne"\n")toif($c eq "\n").