Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

use warnings is complaining

by Anonymous Monk
on Aug 15, 2018 at 13:34 UTC ( #1220371=perlquestion: print w/replies, xml ) Need Help??

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

Hi, i have written a script that reads some data and reports about it. Only when i use warnings i got the following complaint: 'Argument "" isn't numeric in printf at C:\Strawberry\codes\pack.pl line 11, <DATA> line 3.

Here's my code:

use strict; #use warnings; printf("%-11s %-27s %9s %11s","Date","Description","Incoming","Outgoin +g\n"); my $x = 0; my $tot; my $totex; while(<DATA>){ if($x==0){$x++;next;} my($date,$des,$inc,$exp)= unpack("A10 A27 A10 A*",$_); printf("%-10s %-27s %10.2f %10.2f\n",$date,$des,$inc,$exp); $tot += $inc; $totex += $exp; } printf("%38s %10.2f %10.2f","Totals",$tot,$totex); __DATA__ Date Description incoming outgoing 01/24/2001 Zed's Camel Emporium 100.00 1147.99 01/28/2001 Flea spray 24.99 01/29/2001 Camel rides to tourists 235.00 01/31/2001 avage1 125.00 01/20/2001 carpe diem 20.00 23.00

How can i get rid of this, besides from turning warnings off?

Replies are listed 'Best First'.
Re: use warnings is complaining
by Anonymous Monk on Aug 15, 2018 at 13:46 UTC
    Check for empty string and substitute a number instead:
    $inc ||= 0; $exp ||= 0;
    Those expressions transform to $var = $var || 0; and $var is false when it's zero-ish, undef or an empty string. When it's zero, we're not changing anything; we should not get undef in this program; and the empty string the cause of this warning, which we're replacing with 0.
Re: use warnings is complaining
by toolic (Bishop) on Aug 15, 2018 at 13:46 UTC
    Since unpack returns strings of zero length, you can check with length:
    use warnings; use strict; printf("%-11s %-27s %9s %11s","Date","Description","Incoming","Outgoin +g\n"); my $x = 0; my $tot; my $totex; while (<DATA>) { if ($x==0) {$x++;next;} my ($date,$des,$inc,$exp) = unpack("A10 A27 A10 A*", $_); $inc = 0 unless length $inc; $exp = 0 unless length $exp; printf("%-10s %-27s %10.2f %10.2f\n",$date,$des,$inc,$exp); $tot += $inc; $totex += $exp; } printf("%38s %10.2f %10.2f","Totals",$tot,$totex); print "\n"; __DATA__ Date Description incoming outgoing 01/24/2001 Zed's Camel Emporium 100.00 1147.99 01/28/2001 Flea spray 24.99 01/29/2001 Camel rides to tourists 235.00 01/31/2001 avage1 125.00 01/20/2001 carpe diem 20.00 23.00
Re: use warnings is complaining
by SuicideJunkie (Vicar) on Aug 15, 2018 at 13:46 UTC

    Well, like your warning is telling you, " " is not a number.

    So quite simply you need to either make it a number, or not require it to be a number.

    You might do this by testing whether the value is all whitespace, and set it to "-" x 10 or sprintf '%10.2f' based on that test. Then when printing the blended values, use %10s.

Re: use warnings is complaining
by LanX (Sage) on Aug 15, 2018 at 13:42 UTC
    Your data has empty fields, I suppose they are meant to signify zero. So add this after unpacking.
    $inc += 0; $exp += 0;
    Another interpretation is your data is corrupted.

    That's why you get this warning, because Perl can't know what your real intentions are.

    Please note how accurate the warning is, it gives you the data line and the code line...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    Update

    ) I just realized that since you are using unpack you get an empty string not undef (like a regex would).

    Hence adding 0 will continue giving you a warning.

    So better use the $inc ||= 0 approach here, like AnoMonk suggested

Re: use warnings is complaining
by Anonymous Monk on Aug 15, 2018 at 14:02 UTC

    It is ok now, thank you for the solutions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1220371]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-12-09 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?