use strict; use warnings; my %words; my $text = "and him him lad has him done and john has has"; while($text=~/\b(\w+)\b(?=.*\b\1\b)/g){ $words{$1}++; } foreach my $key(keys %words){ my $count = $words{$key} + 1; print "$key: $count\n"; }