in reply to Sort Says "not numeric" then sorts numeric anyway?

I'm pretty sure that what's going on is that the $a <=> $b is forcing its arguments to intify (which they do by prefix, or to 0 if no numeric prefix is found). As you've found, this is often the Right Thing(tm). You can shut warnings.pm up about it (and make it clear to anyone reading your code that you intend to sort them numerically) by using int($a) <=> int($b); int() seems to work just as happily on strings as on reals, and since it returns a number, <=> is happy.

Update: Magical Mystery String Numerification (for values of Magical Mystery equal to "what atof(3) does on this platform") is explained on p. 59 of the 3rd Edition Camel.

Update II: s/strict/warnings/ Thanks jryan!

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!

Replies are listed 'Best First'.
Re: Re: Sort Says "not numeric" then sorts numeric anyway?
by jryan (Vicar) on Feb 11, 2003 at 07:23 UTC

    Strict doesn't cause the warning.

    If it really bothers you, you can shut the warning up by turning the warning off:

    use strict; use warnings; my @x = qw( 1xxx 2xxx 300x 10xx ); @x = sort @x; print "default sort: @x\n"; no warnings 'numeric'; @x = sort {$a <=> $b} @x; print "numeric sort: @x\n";