Win has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way of taking float values presented by MS SQL Server (eg. 2.12121231E-4) and then converting it to a Perl variable that is recognised as a numeric rather than a string?

Replies are listed 'Best First'.
AHA!
by matija (Priest) on Mar 04, 2004 at 12:41 UTC
    I'm posting this at the top, because otherwise it would be lost down in the level 5 of comments to comments to comments. The code (we learned later) looks like this:
    while ( my @col_col = $sthZK_Update_extract->fetchrow() ) { #stuff my $SUM_xi = $col_col[5]; my $SUM_ni = $col_col[6]; # more stuff print "\n^^^^^^\n$Key_m, $Aggregated_area, $Cause, $SUM_wixi, $S +UM_wi, $SU +M_xi, $SUM_ni, $Sum_wi2xi\n"; } my $numerator_L = 2*($SUM_ni-$SUM_xi);
    Remember how I asked you to print the values JUST at the point were you were using them? Because I thought the values were not what you thought they were.

    You are declaring variables with my - giving them scope inside the loop.

    Once you are outside of the loop, the values are GONE.

    If you had put the print statement just before the assign statement, you would have caught it yourself. If you had done use strict; Perl would have caught it for you. That is (one of) the reasons why use strict; is a really, really good idea.

Re: Float values from MS SQL Server.
by matija (Priest) on Mar 04, 2004 at 09:27 UTC
    Hmmmm:
    perl -we '$a="2.12121231E-4"; print $a+0,"\n";' 0.000212121231
    Methinks it is already recognised as a number. Note the lack of any warning, and the fact that the result is correct...
      I think that the problem is that my calculation is having to work with the mumber 422420.0 . The MS SQL Server doesn't hold it like this. Why should the Perl scipt pick it up in this format? I get these kind of messages:
      Use of uninitialized value in subtraction (-) at Creating_table_ZZI_Pa +ul.pl line 1210. Use of uninitialized value in subtraction (-) at Creating_table_ZZI_Pa +ul.pl line 1210. Use of uninitialized value in multiplication (*) at Creating_table_ZZI +_Paul.pl l ine 1211. Use of uninitialized value in addition (+) at Creating_table_ZZI_Paul. +pl line 12 16. Use of uninitialized value in subtraction (-) at Creating_table_ZZI_Pa +ul.pl line 1217. Use of uninitialized value in subtraction (-) at Creating_table_ZZI_Pa +ul.pl line 1217. Invalid n: 0
        I have converted the 422420.0 value to an INT and removed the problem of '422420.0'. But the error messages stay the same. So I guess Perl can handle '422420.0' without any problems. There must be a problem with the equations. The equations take the form:
        my $numerator_L = 2*($SUM_ni-$SUM_xi); + my $denominator_L = 2*$SUM_xi; + my $numerator_U = 2*($SUM_xi+2); + my $denominator_U = 2*($SUM_ni-$SUM_xi);