my %myhash = ( key1 => { map {$_ => 1} qw(abd bcd cde def efg) }, key2 => { map {$_ => 1} qw(abd def} }, key3 => { map {$_ => 1} qw(efg cde tgh) }, ); #### my @winners = grep $myhash{$_}{efg}, keys %myhash; #### my %inside_out; for my $first_key (keys %myhash) { for my $second_key (keys %{$myhash{$first_key}}) { $inside_out{$second_key}{$first_key} = 1; } } for my $second_key (keys %inside_out) { print "$second_key => ", join(", ", sort keys %{$inside_out{$second_key}}), "\n"; }