use strict; my %temphash = ("tom" => "good", "dick" => "bad", "hary" => "ugly"); my $string = 'This string contains tom and dick'; my @occurrences = grep {exists $temphash{$_}} split(/\s/, $string); print scalar @occurrences;