Probably you have "use warnings" turned on. You can disable the warning for
numeric comparison with "no warnings 'numeric';"

perl -E 'use warnings; no warnings "numeric"; my @a =
("12\thi","37\tb","123\tc","187\ta"); my @b = sort { $a <=> $b } @a; say
join("\n",@b)'
12      hi
37      b
123     c
187     a

You can scope this if you like:

  my @result;
  {
    no warnings 'numeric';
    @result = sort { $a <=> $b } @source;
  }

You could also force the string to a number using a thing from Scalar::Util.

References:
  http://perlmaven.com/argument-isnt-numeric-in-numeric
  http://perlmaven.com/automatic-value-conversion-or-casting-in-perl

--Brock


On Tue, Mar 8, 2016 at 4:29 PM, Kenneth Wolcott <kennethwolc...@gmail.com>
wrote:

> Hi;
>
>   How do I call the built-in Perl sort function on an array of strings
> where the string is composed of one or more digits, followed by a tab
> which is followed by a string and I want the results to be sorted in
> reverse numeric order?
>
>   I looked at http://perldoc.perl.org/functions/sort.html and I still
> don't know the right answer to this question.  I suppose if I disable
> warnings, then it works ok?
>
>   The GNU sort (Cygwin/Linux) function does not complain about numbers
> followed by strings for the data when requested with reverse sort and
> just does it.
>
>   The "$b <=> $a" option to sort seems to complain about the strings
> in the data, but the output is correct.
>
> Thanks,
> Ken Wolcott
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to