Edward Wijaya wrote:
Dear Sirs,

I have the following code, that
take Hash of Hash as an iput.

Then I have function (see below) that takes
a hash and a variable as input.
This function will count the elements
of secondary hashes and delete the hash,
if it is below certain variable limit.

So with this :
my %new_hoh = reduce_hash(%hoh,3);

it should return

my %hoh = (
    key2 =>  {    A => 'foo',
                  B => 'bar',
                  C => 'qux'}, );

But my subroutine doesn't work as it should.
Is there anything wrong with it?

Did you not see the warning message "Odd number of elements in hash assignment"? Because your code should produce that warning.



__BEGIN__
use strict;
use warnings;
use Data::Dumper;

my %hoh = (
    key1 =>   {   A => 'foo',
                  B => 'bar',},

    key2 =>  {    A => 'foo',
                  B => 'bar',
                  C => 'qux'},

      key3 =>  {    A => 'foo',}
   );

my %new_hoh = reduce_hash(%hoh,3);
print Dumper \%new_hoh;

#---Subroutine that do the job-------
sub reduce_hash {
    my (%HoH,$limit) = @_;

You are assigning everything in @_ to %HoH and nothing to $limit. Either put the scalar first in the list or pass a reference to the original hash.



    foreach my $k ( keys %HoH ) {
            my $count = 0;
            for my $k2 ( keys %{ $HoH{$k} } ) {
                $count++;
              }
              if ($count < $limit){
              delete $HoH{$k};
        }

No need for $count or the second for loop:

          delete $HoH{ $k } if keys %{ $HoH{ $k } } < $limit;


        }

   return %HoH;
}

__END__


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to