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' };