use Data::Dumper; %input=(r=>[1..10],s=>[1..10],t=>[5..10],u=>[11..20]); grep{$hash{"$input{$_}[0]_$input{$_}[-1]"}++;$tmp{$_}=$input{$_} if $hash{"$input{$_}[0]_$input{$_}[-1]"}==1}keys %input; print Dumper(\%tmp); #### use Data::Dumper; %input=(r=>[1..10],s=>[1..10],t=>[5..10],u=>[11..20]); grep{$hash{$input{$_}[0]}->{$input{$_}[-1]}++;$hash_1{$input{$_}[0]}++;$hash_2{$input{$_}[-1]}++;$tmp{$_}=$input{$_} if ($hash{$input{$_}[0]}->{$input{$_}[-1]}==1 && $hash_1{$input{$_}[0]}==1 && $hash_2{$input{$_}[-1]}==1 )}keys %input; print Dumper(\%tmp); Output: $VAR1 = { 'u' => [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ], 'r' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] };