my %rehash; for my $station ( keys %hash ) { for my $row ( keys %{$hash{$station}} ) { for my $id ( keys %{$hash{$station}{$row}} ) { $rehash{$station}{$id}{$row} = $hash{$station}{$row}{$id}; } } } # now check for matching ids among station2 and station3 for my $s2_key ( keys %{$rehash{"STATION2"}} ) { if ( exists( $rehash{"STATION3"}{$s2_key} )) { # do something... } }