in reply to HOW Can I pass the array into my subroutine?

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

-cheepy-

Replies are listed 'Best First'.
Re^2: HOW Can I pass the array into my subroutine?
by snowsky (Initiate) on Apr 13, 2007 at 14:20 UTC

    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