Parse scientific integer representation in perl

What is the most elegant way to parse an integer specified in a scientific representation, i.e. I have an input file with lines like

value=1.04738e+06 

Of course, I can match all the components (leading digit, decimal positions, exponent) and calculate the result, but it seems to me that there is a more direct way.

+4
source share
2 answers
 % perl -e 'print "1.04738e+06" + 0' 1047380 

You just need to force it to a number, and Perl will be DWIM.

+10
source

FYI: looks_like_number() from Scalar::Util may come in handy.

 #!/usr/bin/env perl use strict; use warnings; use Scalar::Util qw( looks_like_number ); my $line = "value=1.04738e+06"; my ( $tag, $value ) = split /\s*=\s*/, $line, 2; if( looks_like_number( $value ) ){ $value = 0 + $value; } print "$tag=$value\n"; 
+1
source

All Articles