Search code examples
perlfileparagraphs

How can I count paragraphs in text file using Perl?


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");

Solution

  • If you're determining paragraphs by a double-newline ("\n\n") then this will do it:

    open READFILE, "<$filename"
        or die "cannot open file `$filename' for reading: $!";
    my @paragraphs;
    {local $/; @paragraphs = split "\n\n", <READFILE>} # slurp-split
    my $num_paragraphs = scalar @paragraphs;
    __END__
    

    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):

    my $num_paragraphs;
    {local $/; $num_paragraphs = @{[ <READFILE> =~ /\n\n/g ]} + 1}
    

    Although, if you want to keep using your own code, you can change if($C ne"\n") to if($c eq "\n").