use strict; use warnings; use Data::Dumper; my $text = "match other match not useful match sample word"; my $string1 = "match"; my $string2 = "not"; my %repetitions; map {$repetitions{$_}++;} grep /$string1|$string2/, split / /, $text; print Dumper(\%repetitions); ### Outputs: $VAR1 = { 'match' => 3, 'not' => 1 };