use strict; use warnings; use Data::Dump; my %h = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'technology' => 5, 'process' => 6, 'creation' => 7, 'saturation process' => 9, 'automation technology' => 2, 'automation technology process' => 3, ); dd 'before', \%h; for (keys %h) { if (/\s/) { my @l = split ' '; for my $k (map {my $i=$_;map{join" ",@l[$i..$_]}$i..$#l}0..$#l) { delete $h{$k} unless $k eq $_; } } } dd 'after', \%h; __END__ ( "before", { "automation" => 2, "automation technology" => 2, "automation technology process" => 3, "creation" => 7, "mass creation" => 2, "process" => 6, "rendition" => 3, "saturation" => 3, "saturation process" => 9, "technology" => 5, "technology process" => 5, }, ) ( "after", { "automation technology process" => 3, "mass creation" => 2, "rendition" => 3, "saturation process" => 9, }, ) #### use strict; use warnings; use DB_File; use Fcntl; use File::Temp qw(:POSIX); use Data::Dumper; $Data::Dumper::Indent = 1; my $filename = tmpnam(); # temporary file my %h; # tied DB_File hash tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; %h = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3, ); my $prev; for (keys %h) { delete $h{$prev} if $prev && /^$prev/; $prev = $_; } print Dumper(\%h); # cleanup untie %h; unlink $filename; __END__ $VAR1 = { 'automation technology process' => '3', 'mass creation' => '2', 'rendition' => '3', 'saturation' => '3' };