[英]Regex for Evalue substitution in Perl
What I am trying to achieve is convert the Evalue 1e-2 to 0.01. 我想要实现的是将Evalue 1e-2转换为0.01。
my $cutoff = "1e-12";
if ($cutoff =~ m/^\de-{1}\d+?$/){
$cutoff = s/e-/*10^(-/;
$cutoff .= ")";
}
print "$cutoff\n";
This is part of a bigger script and running it under use warnings;
这是一个更大的脚本的一部分,并在
use warnings;
下运行它use warnings;
always gives me Use of uninitialized value $_ in substitution (s///) at test.pl line 4, <STDIN> line 1.
总是给我
Use of uninitialized value $_ in substitution (s///) at test.pl line 4, <STDIN> line 1.
Does anyone spot the mistake here? 有人在这里发现错误吗? I cannot seem to be able to do so.
我似乎无法做到。
The warning you get is because you used =
rather than =~
in front of the substitution operator. 您得到的警告是因为您在替换运算符前面使用了
=
而不是=~
。 You need: 你需要:
$cutoff =~ s/e-/*10^(-/;
But that isn't the only problem here. 但这不是这里唯一的问题。 You would also have to
eval
the statement to get what you wanted, which would not only be a bad design, but completely unnecessary. 您还必须
eval
该语句以获得所需的内容,这不仅是一个糟糕的设计,而且完全没有必要。 Perl natively treats values like "1e-12"
as numbers, so you should not be doing this with a regex at all. Perl本机将像
"1e-12"
类的值视为数字,因此您根本不需要使用正则表达式。 You can simply format the output: 您可以简单地格式化输出:
printf '%d',$val;
That will convert 1e-2
to .01
. 这会将
1e-2
转换为.01
。 If you need to do create very long numbers like this, look into an appropriate module. 如果您需要像这样创建非常长的数字,请查看适当的模块。
Do you realise that "1e-2" is already a valid format for a number in Perl? 您是否意识到Perl中“ 1e-2”已经是数字的有效格式? you just need to persuade Perl to treat it as a number.
您只需要说服Perl将其视为数字即可。
$ perl -E'$x= "1e-2"; say $x'
1e-2
$ perl -E'$x= "1e-2"; $x+=0; say $x'
0.01
Adding zero to it ensures that Perl knows it is a number. 向其添加零可确保Perl知道它是一个数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.