> 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);
>
The above call will not work, because Perl flattens your hash (along
with the 3) into a single argument list. You either need to set the
order of arguments, and collect the hash at the end, or pass by
reference. This is an FAQ, see,
perldoc -q 'How can I pass'
For more.
> 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?
>
> Yours again,
> Edward WIJAYA
> SINGAPORE
>
> __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);
Essentially the above becomes,
my %new_hoh = reduce_hash(\%hoh, 3);
> print Dumper \%new_hoh;
>
> #---Subroutine that do the job-------
> sub reduce_hash {
> my (%HoH,$limit) = @_;
And the above will stick everything in @_ into the hash. Instead it becomes,
my ($HoH, $limit) = @_;
And then you must dereference $HoH as a hash reference.
> foreach my $k ( keys %HoH ) {
> my $count = 0;
> for my $k2 ( keys %{ $HoH{$k} } ) {
> $count++;
> }
> if ($count < $limit){
> delete $HoH{$k};
> }
> }
>
> return %HoH;
> }
>
> __END__
>
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>