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
Gundala Viswanath

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 =3D (
#Yes, features is in AoA
'Arizona'=3D> {
'ZOO_1' =3D> [ '5','HIPPO', ['features1'],['features2']],
'ZOO_2' =3D> [ '10','HIPPO',['features1'],['features2']
],
'ZOO_3' =3D> [ '2', 'PUMA', ['features1'],['features2']],
'ZOO_5' =3D> [ '1', 'PUMA', ['features1'],['features2']],
},
'Indiana' =3D> {
'ZOO_4' =3D> [ '2','HIPPO',['features1'],['features2']]
,
'ZOO_9' =3D> [ '25', 'ZEBRA', ['features1'],['features2']],
'ZOO_5' =3D> [ '13', 'MONKEY',['features1'],['features2']],
'ZOO_6' =3D> [ '23', 'ZEBRA', ['features1'],['features2']],
},
'Nevada' =3D> {
'ZOO_3' =3D> [ '3', 'HIPPO', ['feature1'],['feature2']],
'ZOO_7' =3D> [ '11', 'HIPPO',['feature1'],['feature2']],
'ZOO_4' =3D> [ '21', 'LION', ['feature1'],['feature2']],
'ZOO_12' =3D> [ '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 =3D $bighash{$states}{$zoo}->[1];
my $count =3D 0;

foreach my $nzoo ( keys %{$bighash{$states}} )
{
my $nxt =3D $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 -----
mgoland@optonline.net

2005-08-05, 9:59 pm



----- Original Message -----
From: Gundala Viswanath <gundalav@gmail.com>
Date: Thursday, August 4, 2005 10:20 pm
Subject: Extracting Redundant Elements in HoHoA

> Hi,

Hello,
> 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":

I am not sure what you mean by "Single Best", if you will need more help, please re-define this part
>
> 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.

The problem with your code lay within your data structures. You can save much of your head aiks and many itterations, simply by changing your data structure to HoHoH. Here is one way to do such thing, notice I don't know what you mean by single best, so I
simply printed matching values.

HTH,
Mark G.


#!PERL

use warnings;
use strict;
use Data::Dumper;
my %bighash = (

'Arizona'=> {
'ZOO_1' => {
'HIPPO' , "5",
'features' => [ 'features1','features2' ]
},
'ZOO_2' => {
'HIPPO','10',
'features' => [ 'features1','features2' ]
},
'ZOO_3' => {
'PUMA', '2',
'features' => [ 'features1','features2' ]
},
'ZOO_5' => {
'PUMA', '1',
'features' => [ 'features1','features2' ]
},
},

'Indiana'=> {
'ZOO_4' => {
'HIPPO' , "2",
'features' => [ 'features1','features2' ]
},
'ZOO_9' => {
'ZEBRA','25',
'features' => [ 'features1','features2' ]
},
'ZOO_5' => {
'MONKEY', '13',
'features' => [ 'features1','features2' ]
},
'ZOO_6' => {
'ZEBRA', '23',
'features' => [ 'features1','features2' ]
},
},


'Nevada'=> {
'ZOO_3' => {
'HIPPO' , "3",
'features' => [ 'features1','features2' ]
},
'ZOO_9' => {
'HIPPO','11',
'features' => [ 'features1','features2' ]
},
'ZOO_5' => {
'LION', '21',
'features' => [ 'features1','features2' ]
},
'ZOO_6' => {
'MONKEY', '13',
'features' => [ 'features1','features2' ]

},
'ZOO_7' => {
'Perl', '7',
'features' => [ 'features1','features2' ]

},
}
);




my $set = animal_count(\%bighash);
print_data($set);


sub animal_count(){

my ($ref) = @_;
my ($debug,%global_zoo_count,%matched_hash)
;


foreach my $state ( keys %$ref ){
my %state_count;

foreach my $zoo ( keys %{ $ref->{$state} } ){

foreach my $animal ( keys %{ $ref->{$state}{$zoo} } ){

next if ref $ref->{$state}{$zoo}{$animal}; #skip the features
$matched_hash{$state}->{$animal} = $ref->{$state}{$zoo}{$animal} if ++$state_count{$animal} >= 2 || ++$global_zoo_count{$animal} >= 2;
}

}
}
return \%matched_hash;
}

sub print_data(){
my ($ref) = @_;


#now lets print the results
foreach my $state ( keys %$ref ){
foreach my $animal (keys %{ $ref->{$state} } ){

print "$state $animal $ref->{$state}->{$animal}\n";

}
}
> 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 -----
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

Sponsored Links







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

Copyright 2009 codecomments.com