Professional Web Applications Themes

Problem with subroutines with hash and var as input - PERL Beginners

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; ...

  1. #1

    Default Problem with subroutines with hash and var as input

    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__
    Edward Guest

  2. #2

    Default Re: Problem with subroutines with hash and var as input

    > Dear Sirs, 

    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.
     

    Essentially the above becomes,

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

    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.
     

    http://danconia.org

    Wiggins Guest

  3. #3

    Default Re: Problem with subroutines with hash and var as input

    Edward Wijaya wrote: 

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

     

    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.

     

    No need for $count or the second for loop:

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

     


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  4. #4

    Default Re: Problem with subroutines with hash and var as input

    On Fri, 01 Oct 2004 07:41:43 -0700, John W. Krahn <net> wrote:

    Thanks, 

    John, your one-liner certainly makes my sub
    looks better.
     

    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
    Edward Guest

  5. #5

    Default Re: Problem with subroutines with hash and var as input

    Edward Wijaya wrote: 
    >
    > 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?[/ref]

    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;

    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
    John Guest

  6. #6

    Default Re: Problem with subroutines with hash and var as input

    On Fri, 01 Oct 2004 08:58:44 -0700, John W. Krahn <net> wrote:
     

    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
    Edward Guest

  7. #7

    Default Re: Problem with subroutines with hash and var as input

    Edward Wijaya wrote: 
    >
    > 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.[/ref]

    Sorry, there is an extra $ in there, it should be:

    67: delete $HoH->{ $k } if keys %{ $HoH->{ $k } } < $limit;


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  8. #8

    Default Re: Problem with subroutines with hash and var as input

    On Sat, 02 Oct 2004 01:46:01 -0700, John W. Krahn <net> wrote:

     

    Thanks a lot John.
    Now it's ok.

    RegardS,
    Edward WIJAYA
    SINGAPORE
    Edward Guest

Similar Threads

  1. Hash Problem
    By Camilo in forum ASP.NET Security
    Replies: 6
    Last Post: March 21st, 02:43 AM
  2. Hash problem
    By thewizz in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: February 18th, 02:38 AM
  3. Hash problem
    By Madison in forum PostgreSQL / PGSQL
    Replies: 2
    Last Post: March 12th, 11:29 AM
  4. Sort a hash based on values in the hash stored as arrays of hashes
    By Tore Aursand in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 16th, 10:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139