Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

sorting exponential values in perl

by Anonymous Monk
on Oct 29, 2009 at 05:08 UTC ( #803881=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can we sort a list of exponential values in perl? example:
3.00E-59 8.00E-25 8.00E-47 0 2.3 6.00E-04 5.00E-19 3.00E-10 1.00E-38 4.00E-71 6.00E-70 2.00E-09 2.00E-42 9.00E-14 7.00E-12 0 e-133
how do we sort this list of numbers in ascending order?

Replies are listed 'Best First'.
Re: sorting exponential values in perl
by davido (Archbishop) on Oct 29, 2009 at 06:26 UTC

    From: perlnum:

    Perl can internally represent numbers in 3 different ways: as native integers, as native floating point numbers, and as decimal strings. Decimal strings may have an exponential notation part, as in "12.34e-56" . Native here means "a format supported by the C compiler which was used to build perl".

    If the number is represented in a format supported by the C compiler used to build Perl, it can be treated as a number, which is to say, you can sort it numerically using the numerical comparison operator, <=> (See perlop for an explanation of the flying-saucer operator. ;) ).

    I would only question "e-133" as its status as a number. Is that 0e-133? That's still zero, but being that it starts as a non-numeric character, Perl might treat it as a string. If there is any question as to what Perl sees as a number, you can always use the Scalar::Util module's "looks_like_number()" function to determine what Perl thinks it is.

    Of course there is the convenience with Perl that any string can be treated like a number. If it doesn't make sense as a number, it just evaluates to zero, which may or may not be what you want, but at least is "defined behavior."


Re: sorting exponential values in perl
by ikegami (Pope) on Oct 29, 2009 at 05:08 UTC
Re: sorting exponential values in perl
by DrHyde (Prior) on Oct 29, 2009 at 12:02 UTC
    What have you tried? What happened?
Re: sorting exponential values in perl
by nagalenoj (Friar) on Oct 30, 2009 at 13:55 UTC

    sort function is working values. What's your problem and what have you tried?

    use strict; use warnings; use Data::Dumper; my @arr = (3.00E-59, 8.00E-25, 8.00E-47, 0, 2.3, 6.00E-04, 5.00E-19, 3 +.00E-10, 1.00E-38, 4.00E-71, 6.00E-70, 2.00E-09, 2.00E-42, 9.00E-14, +7.00E-12, 0 ); print Dumper sort @arr;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803881]
Approved by ikegami
Front-paged by biohisham
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-09-23 09:28 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (272 votes). Check out past polls.