#!/bin/perl -w use strict; my %sentence_count; foreach my $sentence() { $sentence=~ s/^\W+//; # remove leading non-words my $counter = split(/\W+/,$sentence); # split on non-words sequence (\W+) # in scalar context split will return # the number of elements in the generated list, # no need to count them ($count= @word in your # example would work too) $sentence_count{($counter)}++; } while ( my($sentence_count,$word_count) = each(%sentence_count)) { print ("There are $word_count sentences of $sentence_count words\n"); } __DATA__ one one two, two two. two two, two. three three three three three three three three three. three three, three