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

Anyone know why this works:
print '' . (sprintf "%5.5d", 1) . "hello\n"; but this doesn't:
print (sprintf "%5.5d", 1) . "hello\n"; and what should I be writing? (I am revolted by that '')

Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: sprintf minor problem
by castaway (Parson) on Sep 03, 2003 at 11:31 UTC
    print sprintf("%5.5d", 1) . "hello\n";
    The second is calling the print function with the results of the sprintf, and then trying to '.' the result of the print fun with "hello\n", throwing away the result.

    (The first one calls sprintf in list context, and makes it a scalar for an argument to print by prepending ''.)

    C.

Re: sprintf minor problem
by gjb (Vicar) on Sep 03, 2003 at 11:33 UTC

    In the second case the sprintf statement is taken as the argument of the print statement, so the statement as a whole concatenates the result of print with the string "hello\n" and prints 00001 as a side effect.

    print((sprintf "%5.5d", 1) . "hello\n");
    will do what you want.

    Incidently, I'd use %05d as a formatting string: this is compatible with C, yours isn't if memory serves (and it doesn't really make sense if you think of it).

    Hope this helps, -gjb-

Re: sprintf minor problem
by jmcnamara (Monsignor) on Sep 03, 2003 at 12:24 UTC

    and what should I be writing?

    Probably something like this:

    printf "%-5d hello\n", 1;

    The folowing formats may also be suitable: %5d, %05d, %5.5f. The format in your example, %5.5d, isn't probably what you want.

    --
    John.

Re: sprintf minor problem
by Hutta (Scribe) on Sep 03, 2003 at 12:20 UTC
    I'm confused as to why you'd use sprintf, then use its return as an argument for print. Why not just use printf to output the entire string you're trying to produce?
    printf("%5.5dhello", 1);
Re: sprintf minor problem
by davido (Cardinal) on Sep 03, 2003 at 17:08 UTC
    In your example:

    print (sprintf "%5.5d", 1) . "hello\n";

    print is seeing the parenthesis as containing the parameters of the print statement, and the . "hello\n" as concatenating "hello\n" to the return value of print.

    The easiest workaround is probably just to use printf in the first place, since that seems to be the behavior you seek. Barring that you can either encapsulate the entire expression to be printed in parenthesis, or put a plus sign just before the (sprintf... so that print understands that to be a portion of an expression, not the parameter list.

    print ( ( sprintf "%5.5d", 1 ) . "hello\n" ); # or print + ( sprintf "%5.5d", 1 ) . "hello\n";

    I like the first solution for clarity, and the second for not having to count opening and closing parenthesis.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      Many thanks all - as well as solving the problem, you've given me some nice stuff to think about.

      Tom Melly, tom@tomandlu.co.uk