Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: passing arrays to subroutines

by g0n (Priest)
on Mar 16, 2005 at 13:08 UTC ( #439929=note: print w/ replies, xml ) Need Help??


in reply to passing arrays to subroutines

Hi,

Best way is to pass a reference to the array, then dereference it in the subroutine. Like this:

use strict; my @array = qw(a b c); mysub(\@array); sub mysub { my @array = @{$_[0]}; foreach (@array) { print $_ } }

Update: Just read Prasad's post. You can just pass the raw array to the subroutine, in which case @_ in the sub is the content of the array. I prefer to use a reference to make it very explicit that this is a single variable - not a sequence of parameters. Neither method is more correct than the other, although Prasads is simpler.

g0n, backpropagated monk


Comment on Re: passing arrays to subroutines
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://439929]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-07-05 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls