Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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?

Comment on sorting exponential values in perl
Download Code
Re: sorting exponential values in perl
by ikegami (Pope) on Oct 29, 2009 at 05:08 UTC
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."


    Dave

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803881]
Approved by ikegami
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-07-28 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls