Gohaku wrote:
>
> Hello everyone,
Hello,
> I am trying to sort an array returned by a subroutine.
Perl's subroutines return a list.
> I realize it would be better if the subroutine returns
> a Sorted Array instead of Sorting the Returned Array, however
> I would like to know why sort doesn't work on a Returned Array and
> why the returned array has to be parenthesized.
>
> #sortarray.pl
> print join("\n",sort arraytest() ),"\n";
>
> #This works --> print join("\n",sort (arraytest()) ),"\n";
> #Don't know why this works --> print join("\n",sort &arraytest()
> ),"\n";
>
> sub arraytest()
> {
> my (@array) = qw( 1 2 3 9 8 7 );
> return @array;
> }
The problem is with the first argument to sort:
perldoc -f sort
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
So:
print join( "\n", sort arraytest() ), "\n";
Is like:
print join( "\n", sort { my @array = qw( 1 2 3 9 8 7 ); return @array } () ), "\n";
Or just:
print join( "\n", sort () ), "\n";
This will work:
print join( "\n", sort +arraytest() ), "\n";
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>