Hashes in subroutines?

Discussion in 'Perl Misc' started by David Fass, Feb 13, 2007.

  1. David Fass

    David Fass Guest

    Ok, stupid question...probably has a really stupid answer that I
    can't see..
    Here's the scenerio I'm pulling data from a database for different time
    periods...so I set up a series of hashes in a subrutine...

    Sorta like this ....

    sub foo($$$$){
    my %hash1 = {};
    my %hash2 = {};
    my %hash3 = {};
    #...pull out data..populate hash1 and hash2 ....#
    # e.g if ($sx eq "M"){ $hash2{$age}++;}
    foreach $key ( sort keys %hash1){
    $hash3->{$key}++;
    }
    foreach $key (sort keys %hash2){
    $hash3->{$key}++;
    }
    foreach $x (sort keys %{$hash3}){
    print "$x\t$hash1{$x}\t$hash2{$x}\n";
    }

    Now the bugger is that I get phantom keys and wierd 0's....
    e.g hypothetically if hash1 is supposed to contain undef for iteration
    1, 22 and 38 for iteration 2, and 46 and 78 for iteration 3...
    I'd get output for iteration 3
    22 0 0
    38 0 0
    46 1 0
    78 0 1
    0 0 0
    0 0 0
    0 0 0

    dumping the hash1 and hash2 using Data:::dump I'd get
    hash1 (46,1,HASH(XXXX),undef);
    hash2(78,1,HASH(XXXX),undef);

    Now how do I zero the hash, including the keys...
    since I keep getting the old keys from previous iterations.

    In a test file I tried using undef(%hash) and %hash=() with similar
    results.

    }
     
    David Fass, Feb 13, 2007
    #1
    1. Advertising

  2. David Fass

    Uri Guttman Guest

    < removed comp.lang.perl which is a VERY dead group no matter what your
    server says.>

    >>>>> "DF" == David Fass <> writes:


    DF> Ok, stupid question...probably has a really stupid answer that I
    DF> can't see..
    DF> Here's the scenerio I'm pulling data from a database for different time
    DF> periods...so I set up a series of hashes in a subrutine...

    DF> Sorta like this ....

    DF> sub foo($$$$){

    don't use prototypes. they don't do what you think they do.


    DF> my %hash1 = {};
    DF> my %hash2 = {};
    DF> my %hash3 = {};

    {} is wrong and not even needed. if you had warnings enabled (use
    warnings) perl would have told you something about those lines.

    DF> #...pull out data..populate hash1 and hash2 ....#
    DF> # e.g if ($sx eq "M"){ $hash2{$age}++;}
    DF> foreach $key ( sort keys %hash1){
    DF> $hash3->{$key}++;
    DF> }

    that is the scalar $hash3 and NOT the hash %hash3. please read
    perlreftut and learn about the differences. you declared a hash but are
    using a hash reference.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 13, 2007
    #2
    1. Advertising

  3. David Fass

    DJ Stunks Guest

    On Feb 12, 7:48 pm, David Fass <> wrote:
    > Ok, stupid question...probably has a really stupid answer that I
    > can't see..
    > Here's the scenerio I'm pulling data from a database for different time
    > periods...so I set up a series of hashes in a subrutine...
    >
    > Sorta like this ....
    >
    > sub foo($$$$){
    > my %hash1 = {};
    > my %hash2 = {};
    > my %hash3 = {};
    > #...pull out data..populate hash1 and hash2 ....#
    > # e.g if ($sx eq "M"){ $hash2{$age}++;}
    > foreach $key ( sort keys %hash1){
    > $hash3->{$key}++;}
    >
    > foreach $key (sort keys %hash2){
    > $hash3->{$key}++;}
    >
    > foreach $x (sort keys %{$hash3}){
    > print "$x\t$hash1{$x}\t$hash2{$x}\n";
    >
    > }
    >
    > Now the bugger is that I get phantom keys and wierd 0's....
    > e.g hypothetically if hash1 is supposed to contain undef for iteration
    > 1, 22 and 38 for iteration 2, and 46 and 78 for iteration 3...
    > I'd get output for iteration 3
    > 22 0 0
    > 38 0 0
    > 46 1 0
    > 78 0 1
    > 0 0 0
    > 0 0 0
    > 0 0 0
    >
    > dumping the hash1 and hash2 using Data:::dump I'd get
    > hash1 (46,1,HASH(XXXX),undef);
    > hash2(78,1,HASH(XXXX),undef);
    >
    > Now how do I zero the hash, including the keys...
    > since I keep getting the old keys from previous iterations.
    >
    > In a test file I tried using undef(%hash) and %hash=() with similar
    > results.
    >
    > }


    you have an error on line 42.

    -jp
     
    DJ Stunks, Feb 13, 2007
    #3
  4. David Fass

    Joe Smith Guest

    David Fass wrote:

    > my %hash1 = {};
    > my %hash2 = {};
    > my %hash3 = {};
    > #...pull out data..populate hash1 and hash2 ....#
    > # e.g if ($sx eq "M"){ $hash2{$age}++;}
    > foreach $key ( sort keys %hash1){
    > $hash3->{$key}++;
    > }
    > foreach $key (sort keys %hash2){
    > $hash3->{$key}++;
    > }
    > foreach $x (sort keys %{$hash3}){
    > print "$x\t$hash1{$x}\t$hash2{$x}\n";
    > }


    Everywhere except on the third line, $hash3 is a reference to a hash.
    On line three, you have a totally different variable: %hash3.

    my $hashref = {};
    $hashref->{$_}++ for keys %hash1;
    $hashref->{$_}++ for keys %hash2;
    print "$_\t$hash1{$_}\t$hash2{$_}\n" for sort keys %{$hashref};

    -Joe
     
    Joe Smith, Feb 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    590
    Ben Holness
    Oct 8, 2003
  2. David Fass

    Hashes in subroutines?

    David Fass, Feb 13, 2007, in forum: Perl
    Replies:
    1
    Views:
    1,936
    Joe Smith
    Feb 13, 2007
  3. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    183
    Mauricio Fernández
    Nov 23, 2005
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    235
  5. deadpickle

    Hashes and subroutines

    deadpickle, Nov 14, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    137
    Mumia W. (reading news)
    Nov 14, 2006
Loading...

Share This Page