Cant return multiple hashes from subroutine

S

sam

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
 
R

Richard Gration

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
 
T

Tad McClellan

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top