----- Original Message ----- From: Gundala Viswanath <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>