Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Answer: How do I pass more than one array or hash to a subroutine

( #2148=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > How do I pass more than one array or hash to a subroutine - Answer contributed by vroom

You need to pass these by reference, otherwise Perl will not see each array or hash as distinct.

You can create a reference by simply prefixing a variable with \ like so:

processarrays(\@array1, \@array2); processhashes(\%hash1, \%hash2);
To use them inside of their routines you can do stuff like the following:
$sub processarrays{ my($a1, $a2) = @_; foreach(@$a1){ # dereferences $a1 print $_; } for($i=0; $i<@$a2; $i++){ print $a2->[$i]; #get at a particular index within array } } sub processhashes{ my($h1,$h2)=@_; foreach my $key(keys %$h1){ #derefences $h1; print $h1->{$key}; #lookup based on $key } }

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2016-07-30 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (268 votes). Check out past polls.