Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-11-25 21:51 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (691 votes), past polls