use warnings; use strict; $/ = ''; my $word = qr/(? ) { while ( $line =~ /($word('$word)?)/g ) { $count{$1}{count}++; $count{$1}{$.}++; $paragraphs = $.; } } for ( sort { $count{$b}{count} <=> $count{$a}{count} || lc $a cmp lc $b } keys %count ) { last if ++$counter > $limit; push @results, $_; } print ' ' x 12; printf "|%12s", $_ for @results; print "\n"; print 'Total count:'; printf "|%12s", $count{$_}{count} for @results; print "\n"; print '-' x ( 13 * ( $limit + 1 ) ), "\n"; for my $line ( 1 .. $paragraphs ) { printf "Prgrph %4s:", $line; printf "|%12s", $count{$_}{$line} || '0' for @results; print "\n"; } __DATA__ "Hello World!" "Oh poor Yorick, his world I knew well yes I did" "don't won't, can't shouldn't, you'll, it's, etc." "Señor Montóya's resüme isn't ápropos." the, the, the, the, the, the, the, the, the, the "Hello World!" "Oh poor Yorick, his world I knew well yes I did" "don't won't, can't shouldn't, you'll, it's, etc." "Señor Montóya's resüme isn't ápropos." the, the, the, the, the, the, the, the, the, the "Hello World!" "Oh poor Yorick, his world knew well yes did" "don't won't, can't shouldn't, you'll, it's, etc." "Señor Montóya's resüme isn't ápropos." the, the, the, the, the, the, the, the, the, the