For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Re: Use of uninitialized value Error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Use of uninitialized value Error
Tom Phoenix

2006-01-10, 4:02 am

On 12/30/05, David Gilden <dowda@coraconnection.com> wrote:

> $_ =3D~ /(\d+)/;
> $num =3D int($1);


If there's no digit in $_, then $1 will be undef (or worse; see
below). I think that's probably your bug: Some filename isn't like the
others. Instead of this:

> my @files =3D<*>;


Consider something like this:

my @files =3D <Gambia*.tiff>;

Later, when you're trying to get the number from the string, you can
allow for the possibility that there isn't one by checking the value
of the pattern match. If it's false, the pattern didn't match, and you
shouldn't use $1.

if (/(\d+)/) {
$num =3D $1;
} else {
warn "No digits found in '$_', continuing...\n";
next;
}

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com