Sergio Escalada wrote:
Hi all! I would like to know if it's possible to make an array sorting with
a subroutine call.

Usually, a sort is made as, for example:


sort {$a <=> $b} @array;


But my intention is something like:


sort subroutine_call @array;

If you really want to call it like this (without assignment to an array), you'll need to use an array reference:

# declare prototype *before* calling it
sub sort_numerical ($);

sort_numerical [EMAIL PROTECTED];
print @array;

sub sort_numerical ($) {
    my $arrayref = shift;
    @{ $arrayref } = sort {$a <=> $b} @{ $arrayref };
}

I think this is ugly; it looks like the sub takes a scalar argument (a reference is a scalar, but not all scalars are arrayrefs).
This will also work:

my @array = qw(3 2 1);

sub sort_numerical (@);

print sort_numerical @array;

sub sort_numerical (@) {
    return sort {$a <=> $b} @_;
}

Personally, I'd drop the prototype and go for:

my @array = qw(3 2 1);

print sort_numerical( @array );

sub sort_numerical {
    return sort {$a <=> $b} @_;
}

HTH
WayPay


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to