in reply to conditional string formatting

I'm not sure this is in any sense "simpler" (it certainly burns a lot more computrons than just checking the length and selecting a format string conditionally), but:

c:\@Work\Perl\monks>perl -wMstrict -le "my @names = qw(A AB ABC ABCD); ;; for my $name (@names) { my $s = sprintf '%4s', sprintf '%-3s', $name; print qq{'$s'}; } " ' A ' ' AB ' ' ABC' 'ABCD'
See sprintf for format specifiers.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^2: conditional string formatting
by Discipulus (Canon) on Jun 08, 2015 at 18:40 UTC
    Wow the double sprintf maneuver
    Perl is limited only by our fantasy.. ++AnomalousMonk

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      It would have been nicer if I could have figured out a way to use a single (s)printf, but I couldn't. :-\


      Give a man a fish:  <%-(-(-(-<

      "double sprintf maneuver "

      Or perhaps The anomalous double sprintf maneuver?

      Sounds a bit like a bizarre chess combination.

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»