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

  2. 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. Advertisements

  3. Mistake, should be
     
    Richard Gration, Jan 6, 2005
    #3

  4. No it doesn't.

    Subroutines in Perl return a "list".


    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.


    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


    perdoc perlreftut
    perdoc perlref
     
    Tad McClellan, Jan 6, 2005
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.