dorp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, according to my manual, perl should be able to parse scientific notation of the form:
(N1)E(N2)
I have a list of scientific numbers. When N2 is positive, it is able to sort them correctly; when negative, the order stays random. Has anyone else noticed this behavior? I'm using Activeperl on a PC. To make it work, I have to convert the negative exponents to positive and then do a reverse sort to trick perl into sorting correctly.

Replies are listed 'Best First'.
Re: Scientific notation problem
by tadman (Prior) on Aug 11, 2001 at 03:31 UTC
    It depends mostly on the method you use to sort. A plain string sort, which is the default, will be easily confused because it tries to sort the numbers alphabetically.

    Here's what I used, and it worked:
    #!/usr/bin/perl my @nums = (2.3e+4, 4e9, 7e-9, 4e-3, 4e-14, 4.4e-14, 4e-13); foreach (sort {$a <=> $b} @nums) { print "$_\n"; }
      I did use something similar. I said:
      foreach(sort byval keys %index) { ... } sub byval { return ($score{$a} <=> $score{$b}) }
      Is there a way to force numerical evaluation?
        That should do it, except that you're evaluating %score on the keys of %index, which may just be an error in your example, or something that should work, but isn't clear from your example.

        You can "force numerical evaluation" with the <=> operator.