use strict; use warnings; my $text = "and him him lad has him done and john has has"; my %words; ++$words{$_} for split /\s+/, $text; print "$_: $words{$_}\n" for sort keys %words; #### and: 2 done: 1 has: 3 him: 3 john: 1 lad: 1