On 7/18/07, Chas Owens <[EMAIL PROTECTED]> wrote:
On 7/18/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:
> How can I detect this, I have been running some code for a few days to
> develop some files and ran into the situation where I am getting the
> following data for input:
>
> 14.95313 14.45312 0
> 14.95313 1.570813E-015 0
> 14.95313 -14.45313 0
> -14.95313 -28.90625 0
> -14.95313 -14.45313 0
> -14.95313 1.570813E-015 0
> -14.95313 14.45312 0
> 14.95313 -28.90625 0
> 0 -28.90625 0
> -14.95313 28.90625 0
> 0 28.90625 0
> 14.95313 28.90625 0
>
> And my code is skipping some lines as it checks for any erroneous data:
> next if grep (/[^0-9.-]/, @data);
> But that thinks the scientific notation is bad. I searched the net and
> didn't find anything. How can I match this specific pattern and convert it?
>
> Thanks!
> jlc
Don't write the regex yourself, use one of the ones in
Regexp::Common*. $RE{num}{real} is probably what you want.
As for how to make "1e3" into 1000, just add 0:
perl -e 'my $i = "1e3"; print $i, " ", $i+0, "\n"';
* http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm
It is important to note that the print function* will use scientific
notation on numbers that are very large or very small. For instance
0.000000000000001570813 will be printed as "1.570813e-15". If you
absolutely must have 0.000000000000001570813 then you will need to use
printf or sprintf:
#!/usr/bin/perl
use strict;
use warnings;
my @n = (
0.000000000000001570813,
10000000000000000000000,
"1e4",
"1e-4"
);
for my $n (@n) {
my $s = expand($n);
print "$n is $s\n";
}
sub expand {
my $n = shift;
return $n unless $n =~ /^(.*)e([-+]?)(.*)$/;
my ($num, $sign, $exp) = ($1, $2, $3);
my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : '';
return sprintf "%${sig}f", $n;
}
* actually any conversion of a number to string for very large or
small numbers, that is why the unless in expand works.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/