Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with numeric insanity???

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
Fellow mongers;

I am trying to extract a series of numbers from a string of numbers using the following code. When I dump out the resulting contents it appears OK but I am getting a non-numeric content error when trying to convert it to numeric.

Can someone please take a look at this snippet to see what I am missing? Or better yet, advise of a better way to extract values into a field/array element containing numeric values?

Source array $XGLA contains 12 x 13 bytes where each segment contains a number OR blanks. I am extracting each segment into a corresponding array element that must be numeric for arithmetic to be performed.

Code Snippet:
# Extract Monthly Amounts
$Y=0;
for($X=0;$X<13;$X++) {
$GLA[$X]=substr($XGLA,$Y,13);
$GLA[$X]=~s/^\s+//;
$GLA[$X]+=0;
$Y+=13;
}

I am receiving the error on the line that contain
$GLA[$X]+=0;

and ANY other line containing arithmetic ops with that field (and others like it).

Advise please???

Signed: Tired programming grunt[sleeping2]
 
Why not just test for the empty variable and assign 0?

Code:
    # Extract Monthly Amounts
    $Y=0;
    for($X=0;$X<13;$X++) {
        $GLA[$X]=substr($XGLA,$Y,13);
        $GLA[$X]=~s/^\s+//;
        unless ($GLA[$X]) { $GLA[$X]=0; }
        $GLA[$X]+=0;
        $Y+=13;
    }

Annihilannic.
 
Can we see some data or is this going to be a guessing game?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
What you are getting is a warning, not an error. And the reason is that you are splitting 13 fields, while you have only 12: the last one is void and causes the warning to be raised.
There is a much shorter (and likely faster) way of doing that:
Code:
$_=$xgla;
@gla=split;
Notice that you don't need to do [tt]$gla[$x]+=0;[/tt] on something that's already numeric (that is: contains only figures and the other allowed characters for numeric representations): perl will not complain (=warn) when using in a numeric operation something that has been generated as a string, but can be directly interpreted as a number.
And if you'll ever need to use in a numeric operation a string that's not fully numeric (e.g.'123abc'), that perl will graciously interpret as the number 123, just say [tt]no warnings;[/tt] before doing that (and restore the warnings immediately after).

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks to Annihilannic for the brain jolt. I used your "unless" suggestion and solved my problem. Guess that's what happens when the brain works too hard for too long!

In response to prex1, I originally tried split but because there can be multiple spaces between the number elements and I was splitting on a space, it was creating many, many elements (one for each space). If you have an expression that will overcome that I'd appreciate seeing it.

Thanks again all.

B
 
Reread my post, BobMCT: after all it is an answer to your questions.
[tt]split;[/tt] , when used without arguments, will split the special var [tt]$_[/tt] on whitespaces (including \t, \n ...) and will treat multiple whitespaces as a single one, also discarding leading and trailing ones. It is a kind of candy offered by perl author to (attentive) programmers.
You'll also find in my post that the solution provided by Annihilannic doesn't solve your mistake: it may suppress the consequences of it, not the mistake itself.
And the mistake is that 0..12 makes 13 elements, but you have only 12 fields in your string. Again, reread carefully my post above.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1;

Thanks for the additional INSIGHT! Obviously I didn't see the reference to 13 (concentrating elsewhere) that you so kindly pointed out. I've reread as much detail as I could on split and did replace all my errant code with that function.

I do have another question for you that I cannot seem to reasonably quickly locate definitive information on and that is:

when using the write() function to output a line defined with a format section, is there a simple way to reference an entire array so it will fill the multiple fields defined?
Hopefully you will have the solution for this?

Thank you -B [bigears]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top