Newbie quesion: Scientific notation

M

mdfoster44

Hi,

I'm trying to upload some data into a MySQL database.

Could someone please advise me how I handle scientific notation?

What I have sofar:

-------
The data (pt, x)

P1 4.50015068000000D-004
-------

My regex expressions, I've been experimenting with:

regex1: P\d+\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/)
does not catch the scientific notation

regex2: P\d+\s+([+-]?(\d+(\.\d*)?|\.\d+)([eEdD][+-]?\d+))/)
does catch the scientific notation. I then say,
$number = $1;

-------
# Insert data
$dbh->do("INSERT INTO data(x) VALUES (?)",undef, $number);
-------

How do I upload the number to the database? If I do it as above it is
incorrect.

Thanks your help.

Martin.
 
A

Anno Siegel

Hi,

I'm trying to upload some data into a MySQL database.

Could someone please advise me how I handle scientific notation?

What I have sofar:

-------
The data (pt, x)

P1 4.50015068000000D-004
-------

My regex expressions, I've been experimenting with:

regex1: P\d+\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/)
^ ^^
Are these supposed to be regex delimiters?
does not catch the scientific notation

Of course not. What makes you think it would?
regex2: P\d+\s+([+-]?(\d+(\.\d*)?|\.\d+)([eEdD][+-]?\d+))/)
does catch the scientific notation. I then say,
$number = $1;

For matching numbers with regular expressions, see perldoc -q "is a number".
Follow the pointers you find there.

Incorrect how? Is the data rejected by the database? Is it accepted
but not the values you expect it to be? Be specific.

Anno
 
M

mdfoster44

Anno said:
Hi,

I'm trying to upload some data into a MySQL database.

Could someone please advise me how I handle scientific notation?

What I have sofar:

-------
The data (pt, x)

P1 4.50015068000000D-004
-------

My regex expressions, I've been experimenting with:

regex1: P\d+\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/)
^ ^^
Are these supposed to be regex delimiters?
does not catch the scientific notation

Of course not. What makes you think it would?
regex2: P\d+\s+([+-]?(\d+(\.\d*)?|\.\d+)([eEdD][+-]?\d+))/)
does catch the scientific notation. I then say,
$number = $1;

For matching numbers with regular expressions, see perldoc -q "is a number".
Follow the pointers you find there.
Ah, this helps alot thanks! I just didn't find that before.
Incorrect how? Is the data rejected by the database? Is it accepted
but not the values you expect it to be? Be specific.
Well the number it uploads is not the float, in this case double, that
I want it to be. I'll need to do a strtod.
Martin.
 
A

Anno Siegel

[...]
Incorrect how? Is the data rejected by the database? Is it accepted
but not the values you expect it to be? Be specific.

Well the number it uploads is not the float, in this case double, that
I want it to be.

Please be specific! Telling us it is not what you want it to be helps
no-one.

What is the data your code offers to the DB? How does the DB receive it?
What does the DB give back, if anything? That's the kind of info we
need.
I'll need to do a strtod.

No, you need it to be what the documentation of your database interface
wants it to be. I have no idea what it expects, but I doubt it's described
as "a strtod":

Anno
 
M

mdfoster44

Anno said:
Anno said:
Hi,
I'm trying to upload some data into a MySQL database.
[...]
How do I upload the number to the database? If I do it as
above it
is
incorrect.

Incorrect how? Is the data rejected by the database? Is it accepted
but not the values you expect it to be? Be specific.

Well the number it uploads is not the float, in this case double, that
I want it to be.

Please be specific! Telling us it is not what you want it to be helps
no-one.

What is the data your code offers to the DB? How does the DB receive it?
What does the DB give back, if anything? That's the kind of info we
need.

The data point
-------
The data (pt, x)

P1 4.50015068000000D-004
-------
From the regex I have sofar,
$number = 4.50015068000000D-004

This is now just a string, I've been incorrectly feeding this to the
database,
where it is expecting a double( double(20,6)).
Which explains why the DB returns
"4.500151"

I need to convert the string stored in $number to a double number.
string to double (strtod). So from perldoc -q "is a number", I've
used:

use POSIX qw(strtod);
my $double = strtod($number);

However, the strtod function doesn't like the "D" in my number, ie
4.50015068000000D-004 needs to be 4.50015068000000e-004. So I do a
quick
substitution.
Thanks for your pointer, sorry for my being vague.

Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top