use strict; use warnings; use Data::Dumper; my %UnNum = ( "forc two bodi twice strong on bodi sai bodi mass" => 35, "you expect on think new bodi made two bodi origin" => 39, "attract bodi b origin force" => 60, "total forc b twice origin force" => 46, "sai on bodi twice mass three time mass forc six" => 34, "on now see why bodi fall same rate bodi twice" => 17 ); my @midWords = qw/bodi forc twice mass acceler law/; my $joined = join("|", @midWords); my $regex = qr/\b$joined\b/; print Dumper(\%UnNum); for my $sentence (keys %UnNum) { $UnNum{$sentence}++ while $sentence =~ /$regex/g; } print Dumper(\%UnNum);