use strict; use warnings; use Data::Dump qw/pp dd/; my %output; my %phrases = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3, 'technology process' => 5, 'automation process' => 2, ); foreach my $keyPhrases (sort hashKeyLength (keys(%phrases))) { my $TermToBeRetained=1; print "\nWorking on * $keyPhrases *\n"; foreach my $keyOutput (keys %output){ print "\tChecking if * $keyPhrases * is contained in * $keyOutput *: "; if ($keyOutput =~/$keyPhrases/){ print "Yes\n"; $TermToBeRetained=0; last; } else{ print "No!\n"; $TermToBeRetained=1; } } if ($TermToBeRetained eq 1){ print "Retaining $keyPhrases\n"; $output{$keyPhrases} = $phrases{$keyPhrases};; } else{ print "Skipping $keyPhrases\n"; } } print "MY RESULT\n"; print pp \%output; sub hashKeyLength { count_units_in_phrase($b) <=> count_units_in_phrase($a) || $b cmp $a } sub count_units_in_phrase { my $string=shift; my $num; $num++ while $string =~ /\S+/g; return $num }