Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie quesion: Scientific notation

Reply
Thread Tools

Newbie quesion: Scientific notation

 
 
mdfoster44@netscape.net
Guest
Posts: n/a
 
      02-01-2005
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.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-01-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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.

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


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

Anno
 
Reply With Quote
 
 
 
 
mdfoster44@netscape.net
Guest
Posts: n/a
 
      02-02-2005

Anno Siegel wrote:
> <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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.
>
> > -------
> > # 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.

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

Martin.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-02-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Anno Siegel wrote:
> > <(E-Mail Removed)> wrote in comp.lang.perl.misc:


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

>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
 
Reply With Quote
 
mdfoster44@netscape.net
Guest
Posts: n/a
 
      02-02-2005
Anno Siegel wrote:
> <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >
> > Anno Siegel wrote:
> > > <(E-Mail Removed)> wrote in comp.lang.perl.misc:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scientific Notation Conversion N1GHTS C++ 1 02-20-2008 07:22 PM
How to represent the scientific notation? asdf C++ 2 10-21-2006 04:12 PM
reguarding scientific notation grinder C Programming 7 08-29-2006 09:26 PM
Scientific Notation Dustan Python 7 12-04-2005 01:56 PM
No scientific notation? Ville Ahonen C++ 2 10-19-2004 10:08 PM



Advertisments