Dear Masters,
Given this hash I want to identify animal that:
1.Occur more than once within the states, AND
2.Occur in more than one states.
In this case the animal is HIPPO.
my %bighash = ( 'Arizona'=> { 'ZOO_1' => [ '5','HIPPO', ['some arr'],['some arr']], 'ZOO_2' => [ '10','HIPPO',['some arr'],['some arr']], 'ZOO_3' => [ '2', 'PUMA', ['some arr'],['some arr']], 'ZOO_5' => [ '1', 'PUMA', ['some arr'],['some arr']], }, 'Indiana' => { 'ZOO_9' => [ '25', 'ZEBRA', ['some arr'],['some arr']], 'ZOO_5' => [ '13', 'MONKEY',['some arr'],['some arr']], 'ZOO_6' => [ '23', 'ZEBRA', ['some arr'],['some arr']], }, 'Nevada' => { 'ZOO_3' => [ '3', 'HIPPO', ['some arr'],['some arr']], 'ZOO_7' => [ '11', 'HIPPO',['some arr'],['some arr']], 'ZOO_4' => [ '21', 'LION', ['some arr'],['some arr']], 'ZOO_12' => [ '13', 'MONKEY',['some arr'],['some arr']], }, );
Such that in the end it simply have:
Arizona ZOO: ZOO_1 ANIMAL: HIPPO ZOO: ZOO_2 ANIMAL: HIPPO NEVADA ZOO: ZOO_3 ANIMAL: HIPPO ZOO: ZOO_7 ANIMAL: HIPPO
What's wrong with my code below such that it doesn't gives the answers correctly?
print Dumper \%bighash ; foreach my $states ( sort keys %bighash ) { print "$states\n"; foreach my $zoo ( keys %{$bighash{$states}} ) { my $cur = $bighash{$states}{$zoo}->[1]; my $count = 0; foreach my $nzoo ( keys %{$bighash{$states}} ) { my $nxt = $bighash{$states}{$nzoo}->[1]; if ( $cur eq $nxt ) { $count++; if ( $count > 1 ) { print "ZOO: $zoo ANIMAL: $nxt\n"; } } } # ----- end foreach ----- } # ----- end foreach ----- } # ----- end foreach -----


---
neversaint and everlastingly indebted.......

In reply to Finding Recurring Elements in HoHoA by neversaint

Title:
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, details, 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, summary, 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.