Re: Problem with subroutines with hash and var as input

2004-10-02 Thread John W. Krahn
Edward Wijaya wrote:
On Fri, 01 Oct 2004 08:58:44 -0700, John W. Krahn [EMAIL PROTECTED] wrote:
Since $HoH now contains a reference to a hash you have to dereference 
it  properly.
 delete $HoH-{ $k } if keys %${ $HoH-{ $k } }  $limit;
 return %$HoH;
I apologize for insisting John.
Tried as you suggested:
64: sub reduce_hash2 {
65:my ($HoH,$limit) = @_;
66:foreach my $k ( keys %${HoH} ) {
67: delete $HoH-{ $k } if keys %${ $HoH-{ $k } }  $limit;
68:}
69:   return %$HoH;
70: }
with this command:
my %new_hoh = reduce_hash2(\%hoh,2);
Gives error:
Not a SCALAR reference at testcode.pl line 67.
Sorry, there is an extra $ in there, it should be:
67: delete $HoH-{ $k } if keys %{ $HoH-{ $k } }  $limit;
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



Re: Problem with subroutines with hash and var as input

2004-10-02 Thread Edward Wijaya
On Sat, 02 Oct 2004 01:46:01 -0700, John W. Krahn [EMAIL PROTECTED] wrote:

there is an extra $ in there, it should be:
67: delete $HoH-{ $k } if keys %{ $HoH-{ $k } }  $limit;
Thanks a lot John.
Now it's ok.
RegardS,
Edward WIJAYA
SINGAPORE
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Problem with subroutines with hash and var as input

2004-10-01 Thread Edward Wijaya
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?
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);
print Dumper \%new_hoh;
#---Subroutine that do the job---
sub reduce_hash {
my (%HoH,$limit) = @_;
foreach my $k ( keys %HoH ) {
my $count = 0;
for my $k2 ( keys %{ $HoH{$k} } ) {
$count++;
  }
  if ($count  $limit){
  delete $HoH{$k};
}
}
   return %HoH;
}
__END__
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Problem with subroutines with hash and var as input

2004-10-01 Thread Wiggins d Anconia
 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




Re: Problem with subroutines with hash and var as input

2004-10-01 Thread John W. Krahn
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



Re: Problem with subroutines with hash and var as input

2004-10-01 Thread Edward Wijaya
On Fri, 01 Oct 2004 07:41:43 -0700, John W. Krahn [EMAIL PROTECTED] wrote:
Thanks,
No need for $count or the second for loop:
John, your one-liner certainly makes my sub
looks better.
Either put the scalar first in the list or pass a reference to the  
original hash.
Now, I also tried with pass by reference
  my ($HoH,$limit) = @_;
foreach my $k ( keys %$HoH ) {   # This two  
don't work
 delete $HoH{ $k } if keys %${ $HoH{ $k } }  $limit;# What's  
wrong with my deref?
}
   return %HoH;

What's wrong with my deref?
Regards
Edward WIJAYA
SINGAPORE
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Problem with subroutines with hash and var as input

2004-10-01 Thread John W. Krahn
Edward Wijaya wrote:
On Fri, 01 Oct 2004 07:41:43 -0700, John W. Krahn [EMAIL PROTECTED] wrote:
Either put the scalar first in the list or pass a reference to the  
original hash.
Now, I also tried with pass by reference
  my ($HoH,$limit) = @_;
foreach my $k ( keys %$HoH ) {   # This two  
don't work
 delete $HoH{ $k } if keys %${ $HoH{ $k } }  $limit;# What's  
wrong with my deref?
Since $HoH now contains a reference to a hash you have to dereference it properly.
delete $HoH-{ $k } if keys %${ $HoH-{ $k } }  $limit;

}
   return %HoH;
return %$HoH;
But you don't really have to return the hash now as you are using a reference 
to the original hash which means that the original hash is modified by your 
subroutine.

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



Re: Problem with subroutines with hash and var as input

2004-10-01 Thread Edward Wijaya
On Fri, 01 Oct 2004 08:58:44 -0700, John W. Krahn [EMAIL PROTECTED] wrote:
Since $HoH now contains a reference to a hash you have to dereference it  
properly.
 delete $HoH-{ $k } if keys %${ $HoH-{ $k } }  $limit;
 return %$HoH;

I apologize for insisting John.
Tried as you suggested:
64: sub reduce_hash2 {
65:my ($HoH,$limit) = @_;
66:foreach my $k ( keys %${HoH} ) {
67: delete $HoH-{ $k } if keys %${ $HoH-{ $k } }  $limit;
68:}
69:   return %$HoH;
70: }
with this command:
my %new_hoh = reduce_hash2(\%hoh,2);
Gives error:
Not a SCALAR reference at testcode.pl line 67.
Regards,
Edward WIJAYA
SINGAPORE
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response