sprintf doesnt work in s///e syntax.

R

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
 
R

raksha34

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.



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
 
D

Dr.Ruud

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top