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.......
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.