use strict; use warnings; my %h = ( 'this is a test' => 2, 'is a test' => 2, 'a test' => 1 ); my @keys = sort {length $a <=> length $b} keys %h; for ( 0 .. $#keys-1){ if ($keys[$_+1] =~ /$keys[$_]/){ delete $h{$keys[$_]}; } }