For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Extracting Redundant Elements in HoHoA









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Extracting Redundant Elements in HoHoA
gundalav@gmail.com

2005-08-04, 10:00 pm

Hi,
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.

Finally sorts that selected animal according to its number.
Such that it finally prints "the single best":

HIPPO, 11
features1
features2

That values is taken from Nevada State, Zoo_7.

The other consideration is if the animal
occur only one time in one state. E.g. HIPPO in Indiana Zoo_4
It is just ignored.

How can I improve my code below such that
it get the result I want? I'm really stuck.
Hope to hear from you guys again.

Thanks,
GundalaV


__BEGIN__

my %bighash = (
#Yes, features is in AoA
'Arizona'=> {
'ZOO_1' => [ '5','HIPPO', ['features1'],['features2']],
'ZOO_2' => [ '10','HIPPO',['features1'],['features2']
],
'ZOO_3' => [ '2', 'PUMA', ['features1'],['features2']],
'ZOO_5' => [ '1', 'PUMA', ['features1'],['features2']],
},
'Indiana' => {
'ZOO_4' => [ '2','HIPPO',['features1'],['features2']]
,
'ZOO_9' => [ '25', 'ZEBRA', ['features1'],['features2']],
'ZOO_5' => [ '13', 'MONKEY',['features1'],['features2']],
'ZOO_6' => [ '23', 'ZEBRA', ['features1'],['features2']],
},
'Nevada' => {
'ZOO_3' => [ '3', 'HIPPO', ['feature1'],['feature2']],
'ZOO_7' => [ '11', 'HIPPO',['feature1'],['feature2']],
'ZOO_4' => [ '21', 'LION', ['feature1'],['feature2']],
'ZOO_12' => [ '13', 'MONKEY',['some arr'],['some arr']],
},
);

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 )
{
# How can I proceed from here?
#print "ZOO: $zoo ANIMAL: $nxt\n";
}
}

} # ----- end foreach -----

} # ----- end foreach -----
} # ----- end foreach -----

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com