Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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; } }
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?

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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://649196]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-06-28 14:48 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (640 votes). Check out past polls.