Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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... } }

In reply to Re: Help me understand hashes by graff
in thread Help me understand hashes by hallikpapa

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2020-11-24 23:28 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found