Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Help me understand hashes

by graff (Chancellor)
on Nov 16, 2007 at 01:41 UTC ( #651107=note: print w/replies, xml ) Need Help??


in reply to Help me understand hashes

What is the importance of $row in the hash structure? If $id is more important than $row, then maybe your structure should be "$hash{$station}{$id}{$row}"...

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... } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://651107]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2020-11-30 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?