Anyway, as it is, you would want to get the ids from all rows of "STATION2", and the ids from all rows of STATION3, and do something for the ones that match. Does it matter whether or not the $row values match too? If not, then you will probably be better off re-ordering the hash layers as suggested in the previous paragraph.
And what sort of "efficiency" are you looking for: run-time speed, or memory footprint? If the structure you showed is essential for some other aspect of your program, but you also want to do this thing with matching ID's, you can either stick with just the one copy of the data in memory and do a lot of extra processing (to navigate through all the rows), or make additional copies of the data (making it easier to work with all the ID's) and do less processing.
Let's suppose there's a good reason to keep the existing structure as-is, and that it's okay to just make a copy of the data so that you can work with the ids. Here's a way to "transpose" the hash layers:
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... } }
In reply to Re: Help me understand hashes
by graff
in thread Help me understand hashes
by hallikpapa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |