Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: passing arrays to subroutines

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


in reply to passing arrays to subroutines

A subroutine gets it's arguments as an array (@_), so if you do

my @array = (1,2,3,4,5); some_sub(@array);
Within some_sub, @_ will equal the contents of @array.
However, you probably want to pass the array into the subroutine by reference (see perlref for more):
my @array = (1,2,3,4,5); some_sub( \@array );
In that case, the first element of @_, the argument list to some_sub, will be a reference to @array.


If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.


Comment on Re: passing arrays to subroutines
Select or Download Code
Re^2: passing arrays to subroutines
by Anonymous Monk on Apr 13, 2011 at 14:22 UTC
    # an array, any array my @bunchOfData = (); # Subroutine: trythis - shows how to pass an array sub trythis { my @array = @_; print "@array\n"; } # where the subroutine is called to do something with the array &trythis(@bunchOfData);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (16)
As of 2015-07-06 20:37 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 (83 votes), past polls