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