At earth time Mon, 29 Dec 2003 06:53:09 -0800, the following transmission
was received from the entity known as Nick Li:
Hi,
I would like to know how to pass a hash to a subroutine, which
evaluates its values and return a value.
Thanks in advance.
Nick
One way is to pass a reference to the hash. The reference is a scalar, so
you can pass it along with other parameters including other hash and array
refs without them getting mixed up. e.g.
my %hash=(key1=>"whatever",
key2=>"something else",
);
$catvals=CatVals(\%hash,"="," ");
print $catvals;
exit;
sub CatVals {
my ($pHash,$eq,$cat)=@_;
@keys=keys(%$pHash);
my ($key,$str);
foreach $key (@keys) {
$str.=$key.$eq.$$pHash{$key}.$cat;
};
$str;
};
outputs:
bash-2.05b$ ./test.pl
key2=something else, key1=whatever,
ALternatively, do 'perldoc -q hash', and look for the section that starts:
Found in /usr/share/perl/5.8.2/pod/perlfaq7.pod
How can I pass/return a {Function, FileHandle, Array, Hash, Method,
Regex}?
With the exception of regexes, you need to pass references to these
objects. See "Pass by Reference" in perlsub for this particular ques-
tion, and perlref for information on references.
andy.