Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

HOW Can I pass the array into my subroutine?

by snowsky (Initiate)
on Apr 12, 2007 at 22:13 UTC ( #609769=perlquestion: print w/ replies, xml ) Need Help??
snowsky has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Do you guys know how i can pass my @array into my sub routine?

&sortArray(@arr); #like this????

Comment on HOW Can I pass the array into my subroutine?
Re: HOW Can I pass the array into my subroutine?
by chromatic (Archbishop) on Apr 12, 2007 at 22:19 UTC

    Exactly like that. Are you having specific trouble?

Re: HOW Can I pass the array into my subroutine?
by ikegami (Pope) on Apr 12, 2007 at 22:20 UTC

    You can only pass a list of scalars to a subroutine. Arrays will automatically get flattened into a list:


    is the same thing as

    mysub($a[0], $a[1], $a[2], ...);

    For example,

    sub sortArray { return sort @_; } my @a = ('e', 'd', 'c', 'b', 'a'); my @sorted = sortArray(@a); print("@sorted\n"); # a b c d e # Same thing. my @sorted = sortArray('e', 'd', 'c', 'b', 'a'); print("@sorted\n"); # a b c d e

    If you want to modify the array, you can pass a reference to the array to the subroutine.

    sub sortArray { my ($ar) = @_; @$ar = sort @$ar; } my @a = qw( e d c b a ); sortArray(\@a); print("@a\n"); # a b c d e

    Some references on references:

    Update: Added reference links.
    Update: Converted first example to sort its arguments.

Re: HOW Can I pass the array into my subroutine?
by GrandFather (Cardinal) on Apr 12, 2007 at 22:28 UTC

    What do you want to do with it? Just what you have (without the &) is fine:

    my @updated = sortArray (@array); ... sub sortArray { my @copy = @_; return sort @copy; }

    if you want to edit the array then passing it by reference may be what you want:

    editArray (\@array); ... sub editArray { my $arrayRef = shift; push @$arrayRef, 'new array element'; }

    There are other variations and the choice of method mostly depends on things like how big the array is and readability of the code. In particular, if you pass an array by reference that may be a strong hint that it will be updated by the subroutine. Passing an array using (@array) is a strong hint that the contents of the array won't be affected by the subroutine.

    DWIM is Perl's answer to Gödel
Re: HOW Can I pass the array into my subroutine?
by educated_foo (Vicar) on Apr 12, 2007 at 22:53 UTC
    You can also use prototypes (if you omit the "&" when you call it), like so:
    sub sortArray(\@) { # Array is in @{$_[0]} } ... sortArray @arr;
    This just implicitly "enreferences" the first argument.
Re: HOW Can I pass the array into my subroutine?
by badaiaqrandista (Pilgrim) on Apr 12, 2007 at 23:26 UTC

    I'm a fan of Params::Validate. It's a great module that help you deal with lack of parameter checking in Perl.

    This is how you declare your module with it:

    use Params::Validate ':all'; sub sortArray { my ($unsorted) = validate_pos(@_, { type => ARRAYREF }); my @sorted = sort @{ $unsorted }; return @sorted; }

    And this is how you can use it:

    my @unsorted = (9, 4, 2, 6, 1); my @sorted = sortArray(\@unsorted);

    You can also use it's validate method for named parameter if your subroutine is likely to get bigger


      Hi Perl Monks!

      I posted a message here was because I didn't find any reference in my reading book that mentioned about passing an array as the argument into the subrutine.

      And i was writing a perl script, which will do exactly what quick sort does. In this script i need write some subrutine as my functions..there is no extra trouble here...

      Anyway, I got all answers I wanted from your great replys.

      Thank you!!

        note that sort does exactly what quick sort does (for some versions of Perl anyway). Unless you are doing this as a learning exercise Perl's built in sort is generally a much better choice than rolling your own.

        DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://609769]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-08-21 10:47 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (134 votes), past polls