rsriram has asked for the wisdom of the Perl Monks concerning the following question:

Hi, When I tried using sprintf to replace my search string, I am encountering a problem. My program line to replace is below
$_ =~ s/<xref t="n(\d+)"\/>/'<xref target="note-'.sprintf{"%02d",$1}.'"\/>'/egs;
But instead of getting the replaced string as a zero-filled two digit number, it displays HASH{x....}. Could anyone help me how to replace the searched string with zero filled number? Thanks

Replies are listed 'Best First'.
Re: Using sprintf
by moritz (Cardinal) on Nov 25, 2008 at 07:49 UTC
    Function calls use round parenthesis in Perl, so just write sprintf(...) instead of sprintf{...}.

    See also perlsyn.

Re: Using sprintf
by graff (Chancellor) on Nov 26, 2008 at 02:41 UTC
    I was going to suggest that you make sure to have use strict; in your code, but then I realized that stricture doesn't help in this case. What the OP code was doing is equivalent to:
    sprintf( {"string1", $string2} )
    and that is not a syntax error or any kind of violation of strictures or warnings, despite the fact that it is something you would never really want to do (i.e. passing a hash reference as the first and only parameter to sprintf).