extracting double from a string

P

perlcoder

We store an array of 32 doubles as a string in a database. When the data is
fetched
from the database it is first read to a char[257] and then converted to an array
of 32 double, by using C union.

Now we want to do the same in perl. We get the data from the database using DBI.
How do you convert from a $scalar to an array.

thanks
 
J

John W. Krahn

perlcoder said:
We store an array of 32 doubles as a string in a database. When the data is
fetched
from the database it is first read to a char[257] and then converted to an array
of 32 double, by using C union.

Now we want to do the same in perl. We get the data from the database using DBI.
How do you convert from a $scalar to an array.

Either //g or split() or unpack()

perldoc perlop
perldoc -f split
perldoc -f unpack



John
 
D

Dr.Ruud

perlcoder schreef:
We store an array of 32 doubles as a string in a database. When the
data is fetched
from the database it is first read to a char[257] and then converted
to an array of 32 double, by using C union.

Now we want to do the same in perl. We get the data from the database
using DBI. How do you convert from a $scalar to an array.

perldoc -f pack (will point you to unpack).

Your doubles seem to be 8 octets each. What is the extra octet for? (a
length value? at the start or end?)

Perl doubles are stored in "native" format, so you need a little luck in
a mixed platform environment, or a search in the CPAN for appropriate
conversion modules.
 
J

Jens Thoms Toerring

perlcoder said:
We store an array of 32 doubles as a string in a database. When the data
is fetched from the database it is first read to a char[257] and then
converted to an array of 32 double, by using C union.

What mean "using a C union"? Are you storing binary values of the
doubles in the database?
Now we want to do the same in perl. We get the data from the database
using DBI. How do you convert from a $scalar to an array.

If the "string" contains binary data for the doubles have a look
at the unpack() function (also see the documentation for pack()
since that contains more relevant information). Without testing
I guess you need

@array = unpack( "d32", $string" );

But since I rarely have used pack/unpack this could be completely
wrong...

Regards, Jens
 
P

perlcoder

perlcoder said:
We store an array of 32 doubles as a string in a database. When the data
is fetched from the database it is first read to a char[257] and then
converted to an array of 32 double, by using C union.

What mean "using a C union"? Are you storing binary values of the
doubles in the database?

Yes we store the binary value of 32 doubles in a char field of the database
of length 256. When we fetch it from the database, we read it to a struct
which contains char[256] union an array of 32 doubles.
[ we are a single platform shop - all Solaris where doubles are 8 bytes ]

If the "string" contains binary data for the doubles have a look
at the unpack() function (also see the documentation for pack()
since that contains more relevant information). Without testing
I guess you need

@array = unpack( "d32", $string" );

But since I rarely have used pack/unpack this could be completely
wrong...

let me try this.

thanks to all who replied.
 
P

Peter J. Holzer

perlcoder said:
We store an array of 32 doubles as a string in a database. When the data
is fetched from the database it is first read to a char[257] and then
converted to an array of 32 double, by using C union.

What mean "using a C union"? Are you storing binary values of the
doubles in the database?

Yes we store the binary value of 32 doubles in a char field of the
database of length 256. When we fetch it from the database, we read it
to a struct which contains char[256] union an array of 32 doubles.
[ we are a single platform shop - all Solaris where doubles are 8 bytes ]

Note that Solaris on SPARC and Solaris on Intel are different platforms
in this regard.

hp

PS: This question seems oddly familiar. Did you already ask it a few
months ago?
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top