Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

avoid uninitialized values for $_

by GertMT (Friar)
on Nov 06, 2007 at 12:18 UTC ( #649196=perlquestion: print w/ replies, xml ) Need Help??
GertMT has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I'm dealing with some csv-files that I use as a basis to make various calculations. It all works fine and the calculations are correct except that while using:
#!/usr/bin/perl -w # use warnings use strict; use diagnostics;
I remain having the problem that I frequently get the message:
Use of uninitialized value in numeric eq (==) at
The reason for these warnings are probably 'missing values' in the csv-file. $_ will be undefined as I understood from documentation. While using things like (as mentioned in an earlier post):
use Data::Table; $joinTable->colsMap( sub { $_->[4] = skipblanks( $_->[4] ) } ); sub skipblanks { my @data = @_; my $x = 0; foreach $x (@data) { if ($x) { $x =~ s/,/\./g; } return $x; } }
or
use Data::Table; $joinTable->colsMap( sub { $_->[4] = to_int_or_zero( $_->[4] ) } ); sub to_int_or_zero { my $value = shift; if ( defined($value) && ( $value =~ m/^\d+$/ || $value =~ m/^-?\d+\.?d*$/ ) ) { return $value; } else { return 0; } }
The message is still appearing sometimes. What am I doing wrong? Maybe not ideal in every situation but what would work for me is to fill all blanks with '0' (zeroes). Is there a way to do this for the whole csv-file at the beginning of the script. Or maybe some other straightforward or generic way to make $_ always initialized in order to avoid the warnings?
Thanks,
Gert

Comment on avoid uninitialized values for $_
Select or Download Code
Replies are listed 'Best First'.
Re: avoid uninitialized values for $_
by lima1 (Curate) on Nov 06, 2007 at 12:34 UTC
    If you just want to avoid the warnings, you can always inhibit them with no warnings. See perllexwarn.
    use warnings; .. # in the block no warnings qw(uninitialized);
    Are you aware that skipblanks() does not iterate over @data? The following code should do the same as your function:
    sub skipblanks { # my @data = @_; # you probably want $x = shift || 0; my $x = shift; # foreach $x (@data) { if ($x) { $x =~ s/,/\./g; } return $x; # } }
      thanks for your reply,
      Very good for pointing me to that guess I shoud have used 'next' or something like that. With your proposed improved skipblanks() it seems to work better! I keep the other suggestion 'no warnings' as a reserve. Thanks,
      Gert

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://649196]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2015-07-08 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls