sprintf doesnt work in s///e syntax.
sprintf doesnt work in s///e syntax.
am 27.12.2007 09:22:11 von raksha34
Hi,
perl -le '
### this part works
$var=sprintf "%d", 0x11;
print $var; ### and we get 17 output
$_ = "{0x11}";
#s/(0x\d+)/hex($1)/e; #### this part works ok and we see
a {17} output
s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
see a {0} output
print;
'
Is this some bug in the sprintf or s///e flag?
Thanks,
Rakesh
Re: sprintf doesnt work in s///e syntax.
am 27.12.2007 11:09:04 von Christian Winter
raksha34@gmail.com wrote:
> Hi,
>
> perl -le '
> ### this part works
> $var=sprintf "%d", 0x11;
> print $var; ### and we get 17 output
>
> $_ = "{0x11}";
>
> #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> a {17} output
>
> s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> see a {0} output
Try
s/0x(\d+)/sprintf("%d",oct($1))/e;
There is a difference between an unquoted mention of 0x11 and a
scalar containing a string of 0x11.
> print;
> '
>
> Is this some bug in the sprintf or s///e flag?
It's expected behaviour.
-Chris
Re: sprintf doesnt work in s///e syntax.
am 27.12.2007 12:04:11 von raksha34
On Dec 27, 3:09 pm, Christian Winter wrote:
> raksh...@gmail.com wrote:
> > Hi,
>
> > perl -le '
> > ### this part works
> > $var=sprintf "%d", 0x11;
> > print $var; ### and we get 17 output
>
> > $_ = "{0x11}";
>
> > #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> > a {17} output
>
> > s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> > see a {0} output
>
> Try
> s/0x(\d+)/sprintf("%d",oct($1))/e;
>
> There is a difference between an unquoted mention of 0x11 and a
> scalar containing a string of 0x11.
>
> > print;
> > '
>
> > Is this some bug in the sprintf or s///e flag?
>
> It's expected behaviour.
>
> -Chris
Thanks for the explanation Chris.
This quoted/unquoted interpretation is unique to hexadecimal/octal
numbers looks like.
Because, we dont see any problems if the variable is anything else.
But why should it
be like that?
I think you meant hex($1) instead of oct($1) in the s///e statement.
> s/0x(\d+)/sprintf("%d",oct($1))/e;
Regards,
--Rakesh
Re: sprintf doesnt work in s///e syntax.
am 27.12.2007 15:22:02 von Christian Winter
raksha34@gmail.com wrote:
> On Dec 27, 3:09 pm, Christian Winter wrote:
> This quoted/unquoted interpretation is unique to hexadecimal/octal
> numbers looks like.
> Because, we dont see any problems if the variable is anything else.
> But why should it be like that?
Well, Perl tries to be as clever as can be without running
too much danger of misinterpreting a string for a number where
it isn't meant to be. The rule of thumb I use to avoid errors
is to convert numbers to base10 as early as possible in a script.
> I think you meant hex($1) instead of oct($1) in the s///e statement.
>> s/0x(\d+)/sprintf("%d",oct($1))/e;
I did. In fact, I managed (not for the first time) to confuse myself
while trying to decide which one to pick. Both hex("11") and oct("0x11")
do the correct thing (hex("0x11") does also). So either works fine:
s/(0x\d+)/sprintf("%d",oct($1))/e;
s/0x(\d+)/sprintf("%d",hex($1))/e;
-Chris
Re: sprintf doesnt work in s///e syntax.
am 28.12.2007 00:17:15 von rvtol+news
raksha34@gmail.com schreef:
> perl -le '
> ### this part works
> $var=sprintf "%d", 0x11;
> print $var; ### and we get 17 output
>
> $_ = "{0x11}";
>
> #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> a {17} output
>
> s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> see a {0} output
>
> print;
> '
Ask perl for help, by adding -w.
perl -Mstrict -we'
printf "%d\n", "0" . "\x11";
'
> Is this some bug in the sprintf or s///e flag?
No, PEBCAK.
--
Affijn, Ruud
"Gewoon is een tijger."