Passing a hash to a function

M

Mark Healey

I can't seem to find in in my perl book. I'd like to pass hashes to
functions

#!/usr/bin/perl -wT

my %someHash;

$someHash{'one'} = "1";
$someHash{'two'} = "2";
$someHash{'three'} = "3";


sub onsiestwosies
{
do stuff with %someHash
}

I'd simply like to pass %someHash to onesiestwosies.
 
T

Tassilo v. Parseval

Also sprach Mark Healey:
I can't seem to find in in my perl book. I'd like to pass hashes to
functions

#!/usr/bin/perl -wT

my %someHash;

$someHash{'one'} = "1";
$someHash{'two'} = "2";
$someHash{'three'} = "3";


sub onsiestwosies
{
do stuff with %someHash
}

I'd simply like to pass %someHash to onesiestwosies.

Under well defined circumstances, you can simply do it like this:

sub func {
my %hash = @_;
}
...
func(%someHash);

The problem with that is that it a) copies the hash (which may be
inefficient if it is large) and b) you can't pass more than one list-ish
variable to the subroutine (perl flattens all arguments passed into one
large list). So best is to use a reference:

sub func {
my ($hash1_ref, $hash2_ref) = @_;
# do the dereferencing
my %hash1 = %$hash1_ref;
...
}
...
func(\%someHash1, \%someHash2);

See perlreftut.pod and perlref.pod if you aren't acquainted with
references. All about subroutines (including how to pass arguments and
access them), can be found in perlsub.pod.

Not sure whether you are familiar with the perldocs. You only mention a
book. The documentation of Perl is often more suitable for looking up such
things. See 'perldoc perl' for a list of available manpages and 'perldoc
perldoc' on how to access the Perl documentation.

Tassilo
 
M

Mark Healey

Also sprach Mark Healey:


Under well defined circumstances, you can simply do it like this:

sub func {
my %hash = @_;
}
...
func(%someHash);

The problem with that is that it a) copies the hash (which may be
inefficient if it is large) and b) you can't pass more than one list-ish
variable to the subroutine (perl flattens all arguments passed into one
large list). So best is to use a reference:

sub func {
my ($hash1_ref, $hash2_ref) = @_;
# do the dereferencing
my %hash1 = %$hash1_ref;
...
}
...
func(\%someHash1, \%someHash2);

See perlreftut.pod and perlref.pod if you aren't acquainted with
references. All about subroutines (including how to pass arguments and
access them), can be found in perlsub.pod.

Not sure whether you are familiar with the perldocs. You only mention a
book. The documentation of Perl is often more suitable for looking up such
things. See 'perldoc perl' for a list of available manpages and 'perldoc
perldoc' on how to access the Perl documentation.

Thanks. Now I have to figure out how flexible I want the function to
be.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top