Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Masters,
My question here is an extention to my earlier postings. In particular to the answer given by frodo72.

I am extending the very first block of his code. The intention is to include variations of the animal during the counting. Such that given this:
my %bighash = ( 'Arizona'=> { 'ZOO_1' => [ '5','HIPPO1', ['feat1'],['feat2']], 'ZOO_2' => [ '10','HIPPO2',['feat1'],['feat2']], 'ZOO_3' => [ '2', 'PUMA1', ['feat1'],['feat2']], 'ZOO_5' => [ '13', 'PUMA2',['feat1'],['feat2']], }, 'Indiana' => { 'ZOO_2' => [ '10','HIPPO3', ['feat1'],['feat2']], 'ZOO_9' => [ '25','ZEBRA1', ['feat1'],['feat2']], 'ZOO_5' => [ '13','MONKEY1', ['feat1'],['feat2']], 'ZOO_6' => [ '23','ZEBRA2', ['feat1'],['feat2']], 'ZOO_3' => [ '2', 'PUMA3', ['feat1'],['feat2']], 'ZOO_5' => [ '1', 'PUMA4', ['feat1'],['feat2']], }, 'Nevada' => { 'ZOO_3' => [ '3', 'HIPPO4', ['feat1'],['feat2']], 'ZOO_7' => [ '11', 'HIPPO5', ['feat1'],['feat2']], 'ZOO_4' => [ '21', 'LION1', ['feat1'],['feat2']], 'ZOO_12' => [ '13','MONKEY2',['feat1'],['feat2']], }, );
My code below:
my %animals; my @zoos; foreach my $state (keys %bighash) { foreach my $zoo (keys %{$bighash{$state}}) { foreach my $state2 (keys %bighash) { foreach my $zoo2 (keys %{$bighash{$state2}}) { my $animal1 = $bighash{$state}{$zoo}[1]; my $animal2 = $bighash{$state2}{$zoo2}[1]; if ( is_variation($animal1,$animal2) == 1 ) { #print "$animal1 - $animal2\n"; push @zoos, $zoo; } push @{$animals{$animal1}{$state}}, [@zoos]; } } } } print Dumper \%animals ; sub is_variation { #Test if two animals are variation of others: #e.g. HIPPO1 - HIPPO2 -> True # HIPPO1 - MONKEY2 -> False my ($s1,$s2) = @_; my ($t1,$t2); $s1 =~ /([A-Za-z]+)(\d+)$/; $t1 = $1; $s2 =~ /([A-Za-z]+)(\d+)$/; $t2 = $1; #print "$t1-$t2\n"; if ( $t1 eq $t2 ) { return 1; } return 0; }
Should've given this:
__END__ $VAR1 = { 'HIPPO1' => { 'Arizona'=> ['ZOO_1', 'ZOO_2'], # from Zoo1_1 HIPPO1 and Zoo_2 HIPPO2 'Indiana'=> ['ZOO_2'], 'Nevada' => ['ZOO_3', 'ZOO_7'], } # So the hash above contain list of States and # Zoos that own variations of Hippo (HIPPO1..5) # Same thing apply for animals below: 'HIPPO2' => { 'Arizona'=> ['ZOO_1', 'ZOO_2'], 'Indiana'=> ['ZOO_2'], 'Nevada' => ['ZOO_3', 'ZOO_7'], } 'HIPPO3' => { 'Arizona'=> ['ZOO_1', 'ZOO_2'], 'Indiana'=> ['ZOO_2'], 'Nevada' => ['ZOO_3', 'ZOO_7'], } 'HIPPO4' => { 'Arizona'=> ['ZOO_1', 'ZOO_2'], 'Indiana'=> ['ZOO_2'], 'Nevada' => ['ZOO_3', 'ZOO_4'], } 'HIPPO5' => { 'Arizona'=> ['ZOO_1', 'ZOO_2'], 'Indiana'=> ['ZOO_2'], 'Nevada' => ['ZOO_3', 'ZOO_4'], } 'PUMA1' => { 'Arizona'=> ['ZOO_3', 'ZOO_5'], 'Indiana'=> ['ZOO_3', 'ZOO_5'], } 'PUMA2' => { 'Arizona'=> ['ZOO_3', 'ZOO_5'], 'Indiana'=> ['ZOO_3', 'ZOO_5'], } 'PUMA3' => { 'Arizona'=> ['ZOO_3', 'ZOO_5'], 'Indiana'=> ['ZOO_3', 'ZOO_5'], } 'PUMA4' => { 'Arizona'=> ['ZOO_3', 'ZOO_5'], 'Indiana'=> ['ZOO_3', 'ZOO_5'], } 'MONKEY1' => { 'Indiana'=> ['ZOO_5'], 'Nevada'=> ['ZOO_12'], } 'MONKEY2' => { 'Indiana'=> ['ZOO_5'], 'Nevada'=> ['ZOO_12'], } 'ZEBRA1' => { 'Indiana'=> ['ZOO_6','ZOO_9'], } 'ZEBRA2' => { 'Indiana'=> ['ZOO_6','ZOO_9'], } 'LION1' => { 'Nevada'=> ['ZOO_4'], } };
But it doesn't. Is there anything I miss in my code there?

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

In reply to Counting Variations of Key in a Hash 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, 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2020-05-29 10:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (169 votes). Check out past polls.

      Notices?