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
Adriano Ferreira

2006-01-10, 4:02 am

On 12/30/05, David Gilden <dowda@coraconnection.com> wrote:
> In the Script below the line: last if ($num >=3D 35)
> is giving me this error: Use of uninitialized value in int


That's not an error, but a warning. You will find that execution goes
after this.

> How do I avoid this error?

@files probably contain a name which does not match /(\d+)/. In this
case, $1 turns to be undef, and so happens with $num (because
int(undef) -> undef) up to the numeric comparison which (under -w)
emits the warning.

To avoid the warning, maybe you don't need to process such filenames

...
$_ =3D~ /(\d+)/;
next unless $1; # skip to the next item
$num =3D int($1);
...

or you consider $num as 0 in this case, by replacing C<$num =3D int($1)>
with C<$num =3D int($1 || 0)

>
>
> my @files contains: Gambia001.tiff through Gambia100.tiff
>
> #!/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);
> # rename($old,$_);
> }

Sponsored Links







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

Copyright 2009 codecomments.com