Home > Archive > PERL Beginners > January 2006 > 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 |
Use of uninitialized value Error
|
|
| David Gilden 2006-01-10, 4:02 am |
| Hello,
In the Script below the line: last if ($num >=3D 35)
is giving me this error: Use of uninitialized value in int=20
How do I avoid this error?
my @files contains: Gambia001.tiff through Gambia100.tiff=20
#!/usr/bin/perl -w
my @files =3D<*>;
$tmp=3D 1;
for (@files){
my $old =3D $_;
$_ =3D~ /(\d+)/;
$num =3D int($1);
#$_ =3D~s/Gambia_Pa_Bobo_kuliyo_\d+/Gambia_Pa_Bobo_kuliyo_$tmp/i;
print "$num\n";
#$tmp++;
last if ($num >=3D 35);=20
# rename($old,$_);
}
Thx,
Dave GIlden
(kora musician / audiophile / webmaster @ www.coraconnection.com / Ft. Wor=
th, TX, USA)
| |
| JupiterHost.Net 2006-01-10, 4:02 am |
|
David Gilden wrote:
> Hello,
Hello,
> In the Script below the line: last if ($num >= 35)
> is giving me this error: Use of uninitialized value in int
>
> How do I avoid this error?
a) Initialize it :)
my $num = 0;
b) don't use warnings - but don't do that
c) turn off uninitialized wanrings (see perldoc warnings) - but don't do
that either
Also please use strict, expecially on "problem" code you're posting to
the list for help on. IN this case you waould have gotten an error about
this unless I looked at it wrong.
HTH
| |
| John W. Krahn 2006-01-10, 4:02 am |
| David Gilden wrote:
> Hello,
Hello,
> In the Script below the line: last if ($num >= 35)
> is giving me this error: Use of uninitialized value in int
>
> How do I avoid this error?
You are using the results of a regular expression match without verifying that
the regular expression matched successfully. Don't do that.
> my @files contains: Gambia001.tiff through Gambia100.tiff
>
> #!/usr/bin/perl -w
>
> my @files =<*>;
> $tmp= 1;
>
>
> for (@files){
> my $old = $_;
> $_ =~ /(\d+)/;
> $num = int($1);
if ( /(\d+)/ ) {
$num = int $1;
}
Or perhaps:
next unless /(\d+)/;
$num = int $1;
Or maybe only work on files that actually have digits in them:
my @files = <*[0-9]*>;
> #$_ =~s/Gambia_Pa_Bobo_kuliyo_\d+/Gambia_Pa_Bobo_kuliyo_$tmp/i;
> print "$num\n";
> #$tmp++;
> last if ($num >= 35);
> # rename($old,$_);
> }
John
--
use Perl;
program
fulfillment
|
|
|
|
|