简体   繁体   中英

Regex for Evalue substitution in Perl

What I am trying to achieve is convert the Evalue 1e-2 to 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; always gives me 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. Perl natively treats values like "1e-12" as numbers, so you should not be doing this with a regex at all. You can simply format the output:

printf '%d',$val;

That will convert 1e-2 to .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? you just need to persuade Perl to treat it as a number.

$ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM