develooper Front page | perl.perl5.porters | Postings from December 2004

Re: [perl #32915] "Use of uninitialized value in substitution iterator" while evaluating a string that match /0+[89]\d+/

From:
Yitzchak Scott-Thoennes
Date:
December 8, 2004 10:23
Subject:
Re: [perl #32915] "Use of uninitialized value in substitution iterator" while evaluating a string that match /0+[89]\d+/
Message ID:
20041208182325.GA612@efn.org
On Mon, Dec 06, 2004 at 09:07:15PM -0000, Tobias Spranger wrote:
> I tried to write a parser, that can do some basic math operations with numbers 
> in a text file. If I execute this exaple program ...
> ------------8<------------
> #!/usr/bin/perl -w
> 
> my $s= qr/[\+\-\/\*]/;
> my $b=1000;
> for (my $i=0; $i < 10; $i++)
> {
>   for (my $j=0; $j < 10; $j++)
>   {
>     my $a="some Text +${i}${j} some Other Text\n";
>     $a =~s/($s[0-9]+)/$b.$1/ee;
>     print $a ;
>   }
> }

As Paul said, you are evaluating perl code that looks like: 1000+08,
and perl treats numbers with leading 0's as octal constants.  However,
this only applies to numbers found in code perl is evaluating/
compiling, *not* to implicit string/number conversion.  So you could
do this instead:

    $a =~s/($s)([0-9]+)/qq!$b$1"$2"!/ee;

which evals 1000+"08" instead, and results in 1008.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About