如何在 Perl 中将像 3 10^-20 这样的字符串转换为数字? [英] How can I convert a string like 3 10^-20 into a number in Perl?
本文介绍了如何在 Perl 中将像 3 10^-20 这样的字符串转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含数字列表的文件,如下所示:
I have a file that contain list of numbers that looks like this:
10^-92
2 10^-14
10^-105
3 10^-20
进一步解释一下 10^-92
本质上是 1E-92
而 2 10^-14
是 2E^-14
.是否有一种紧凑的方法可以将上述文件中的数字转换为 Perl 数字?最后,我想按数字对这些数字进行排序.
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.
推荐答案
如果您的所有数字都需要相同的转换才能成为 Perl 的数字,为什么不使用正则表达式替换:
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
这篇关于如何在 Perl 中将像 3 10^-20 这样的字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文