Cant return multiple hashes from subroutine

Discussion in 'Perl Misc' started by sam, Jan 6, 2005.

  1. sam

    sam Guest

    Hi group,

    The following code only return a single hash:

    #!/usr/bin/perl -w

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

    sub construct
    {
    my %values = ();
    my %valuesB = ();
    my $sales_total = 200;
    my $sales_totalB = 400;
    $values{'sales_calc_total'} = ($sales_total);
    $valuesB{'sales_calc_total'} = ($sales_totalB);
    return (%values, %valuesB);
    }

    my(%hashh, %hashhB) = construct;
    print Dumper(%hashh);
    print "\n";
    print Dumper(%hashhB);
    print "sales: $hashh{'sales_calc_total'}\n";

    Result:
    # !perl
    perl test5.pl
    $VAR1 = 'sales_calc_total';
    $VAR2 = 400;

    sales: 400

    But where is hashh?

    I've been scratching my head for whole day but couldn't found a way to
    fix it. It does look simple, but it turns out killing me completely. :(

    Thanks
     
    sam, Jan 6, 2005
    #1
    1. Advertising

  2. On Thu, 06 Jan 2005 23:38:44 +0800, sam wrote:

    > Hi group,
    >
    > The following code only return a single hash:


    Actually, it returns a single *list*, which in this case is successive
    key,value pairs *from*both*hashes*.

    All information you need is in "perldoc perlref", but the short version is
    that you need to return *references* to hashes, not the hashes themselves.

    #!/usr/bin/perl -w

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

    sub construct
    {
    my %values = ();
    my %valuesB = ();
    my $sales_total = 200;
    my $sales_totalB = 400;
    $values{'sales_calc_total'} = ($sales_total);
    $valuesB{'sales_calc_total'} = ($sales_totalB);
    return (\%values, \%valuesB);
    ^ ^
    }

    # This line incorrect now, have to pick up hash refs
    #my(%hashh, %hashhB) = construct;

    my ($hashr1, $hashr2) = construct;
    ^ ^

    # this block now must use hash refs, not hashes
    #print Dumper(%hashh);
    #print "\n";
    #print Dumper(%hashhB);
    #print "sales: $hashh{'sales_calc_total'}\n";

    print Dumper($hashr1);
    ^
    print "\n";
    print Dumper($hashr2);
    ^
    print "sales: $hashh->{'sales_calc_total'}\n";
    ^^

    HTH
    Rich
     
    Richard Gration, Jan 6, 2005
    #2
    1. Advertising

  3. On Thu, 06 Jan 2005 16:06:07 +0000, Richard Gration wrote:
    > ^
    > print "sales: $hashh->{'sales_calc_total'}\n";
    > ^^


    Mistake, should be

    > print "sales: $hashr1->{'sales_calc_total'}\n";
     
    Richard Gration, Jan 6, 2005
    #3
  4. sam <> wrote:


    > The following code only return a single hash:



    No it doesn't.

    Subroutines in Perl return a "list".


    > return (%values, %valuesB);



    That returns a list of all of the key/value pairs from %values
    followed by all of the key/value pairs from %valuesB.

    You cannot tell where the "boundary" between the 2 are.


    > my(%hashh, %hashhB) = construct;
    > print Dumper(%hashh);


    > print "sales: $hashh{'sales_calc_total'}\n";




    > I've been scratching my head for whole day but couldn't found a way to
    > fix it.



    You need to return a (2-element) list of references instead of
    smushing together all of the key/value pairs.

    return (\%values, \%valuesB); # take references

    ...

    my($h, $hB) = construct; # save references returned

    print Dumper($h); # no de-referencing required

    print "sales: $h->{'sales_calc_total'}\n"; # de-reference the hash


    > It does look simple, but it turns out killing me completely. :(



    perdoc perlreftut
    perdoc perlref


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 6, 2005
    #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. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
  2. Replies:
    5
    Views:
    116
    Chris Mattern
    May 7, 2005
  3. 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:
    233
  4. king
    Replies:
    5
    Views:
    210
  5. Patrick Hartman

    weird behavior when passing hashes to subroutine

    Patrick Hartman, Feb 19, 2010, in forum: Perl Misc
    Replies:
    9
    Views:
    134
    Jens Thoms Toerring
    Feb 19, 2010
Loading...

Share This Page