my ( %wordcount, $relevancy ); for ( split /\W+/, $string ) # gets a list of all words { $wordcount{$_}++; } foreach my $word ( @list_of_words ) { $relevancy += $wordcount{$word}; }