Prev: FAQ 8.26 Why doesn't open() return an error when a pipe open fails?
Next: Detecting Bourne (or csh) shell from Perl
From: darkon on 3 Mar 2010 16:55 mdudley <mdudley(a)king-cart.com> wrote: > Is there any way to sort floating point numbers in perl? Every > example I have tried has failed. Numerical sort appears to NOT to > be a numerical sort but rather an integer sort. I suspect that reading the docs for sort() will help. There are even examples.
From: John Bokma on 3 Mar 2010 16:55 Steve C <smallpond(a)juno.com> writes: > mdudley wrote: >> Is there any way to sort floating point numbers in perl? Every >> example I have tried has failed. Numerical sort appears to NOT to be >> a numerical sort but rather an integer sort. >> > > worksforme > perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))' > 0.1 > 0.25 > 0.3 > 0.5 > 0.7 Coincidence. That's why you must read documentation... perl -e 'print join("\n", sort ( 0.0001, 0.000002, 0.1 ))' 0.0001 0.1 2e-06 Note that sort defaults to string compare: perl -e 'print join("\n", sort { $a <=> $b } ( 0.0001, 0.000002, 0.1 ))' 2e-06 0.0001 0.1 -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development
From: Tad McClellan on 3 Mar 2010 16:56
Steve C <smallpond(a)juno.com> wrote: > mdudley wrote: >> Is there any way to sort floating point numbers in perl? Every >> example I have tried has failed. Numerical sort appears to NOT to be >> a numerical sort but rather an integer sort. >> > > worksforme > perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))' > 0.1 > 0.25 > 0.3 > 0.5 > 0.7 Doesn't work for me... perl -e 'print join ("\n", sort (2.0, 12.0))' 12 2 Your's is not a numeric sort at all, it is a stringwise sort. perl -e 'print join ("\n", sort {$a <=> $b} (2.0, 12.0))' 2 12 -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site. |