in reply to •Re: Can you change the format pad character?
in thread Can you change the format pad character?

At least when the padding is whitespace (which the OP seemed to want), is this solution better than using (s)printf? For ex:

my $string = 'foo'; my $len = 15; $string = sprintf "%${len}s", $string; print $string;

Please note: Don't get me wrong. For fear of fire and brimstone, the Novice that I am would not presume to criticise merlyn. It's just that I've been playing with (s)printf recently, prompting me to change a few old progs from, like:

print ' ' x ( 23 - length $foo ), $foo;

to:

printf '%23s', $foo;

Have I been I wrong or (more likely), AIMS (Am I Missing Something)?

dave

Replies are listed 'Best First'.
•Re: Re: •Re: Can you change the format pad character?
by merlyn (Sage) on May 27, 2003 at 20:23 UTC
    You said:
    At least when the padding is whitespace (which the OP seemed to want)
    But OP said:
    Is it possible to modify the default pad character of the format function from space to something user defined?
    Yes, had the original question been about padding whitespace, I certainly would have preferred sprintf. But it was precisely not about that, hence my solution.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Doh!

      How did I manage to read the exact opposite of what was said?

      Sorry...

      dave