How can I convert a string like 3 10^-20 into a number in Perl?

自作多情 提交于 2019-12-23 14:04:05

问题


I have a file that contain list of numbers that looks like this:

10^-92
2 10^-14
10^-105
3 10^-20

To explain a bit further 10^-92 is essentially 1E-92 and 2 10^-14 is 2E^-14. Is there a compact way to convert the number in above file into Perl number? At the end I want to sort these numbers numerically.


回答1:


If all your numbers require the same transform to become numeric to Perl, why not use a regex substitution:

use 5.010;

my @nums = ('10^-92', '2 10^-14', '10^-105', '3 10^-20');

s{ (\d*) \s* 10\^ }{ ($1 || 1) . 'e' }xe for @nums;

say "@nums";  # 1e-92 2e-14 1e-105 3e-20



回答2:


If you want a Perl number, just go straight to the number without the eval. Store the parts of the number that you extract and use them in a cached-key sort:

use 5.010;

while( <DATA> )
    {
    chomp;

    m/
        (?:
            (?<coefficient> \d+ (?:\.\d+)? )
            \s+
        )?

        (?<base> (\d+) )

        \^

        (?<exponent> -?\d+ )
    /x;


    push @numbers,
        [ ( $+{coefficient} // 1 ), $+{exponent}, $_ ];
    }

my @sorted = sort {
    $a->[1] <=> $b->[1]
        ||
    $a->[0] <=> $b->[0]
    } @numbers;

foreach my $number ( @sorted )
    {
    print "$number->[-1]\n";
    }

__DATA__
10^-92
2 10^-14
10^-105
3 10^-20

You can boil that down to a Schwartzian Transform:

use 5.010;

print
map {
    $_->[-1]
    }
sort {
    $a->[1] <=> $b->[1]
        ||
    $a->[0] <=> $b->[0]
    }
map {
    m/
        (?:(?<c> \d+ (?:\.\d+)? ) \s+ )? # coefficient
        (?<b> (\d+) )                    # base
        \^
        (?<e> -?\d+ )                    # exponent
    /x;
    [ ( $+{c} // 1 ), $+{e}, $_ ];
    } <DATA>;


__DATA__
10^-92
2 10^-14
10^-105
3 10^-20



回答3:


$ perl -ne 's/ /*/;s/\^/E/;print "\n".eval $_' file

2e-13
1e-91
2e-13
1e-104
3e-19


来源:https://stackoverflow.com/questions/2642149/how-can-i-convert-a-string-like-3-10-20-into-a-number-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!