in reply to Re: How to right align outputs of stored data in a variable?
in thread How to right align outputs of stored data in a variable?

... a minimum field width. ... that ... will be exceeded to print what is necessary. ... an explicit space in the format spec to guarantee that the printout will have a space between columns.

A good point. I guess I was distracted by the fact that the field labels already had semicolons to delimit the name fields. :)


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

Replies are listed 'Best First'.
Re^3: How to right align outputs of stored data in a variable?
by Marshall (Canon) on Feb 06, 2017 at 00:36 UTC
    No problem. Your post++ also illustrated a good point that might be missed by many... The "format string" can be calculated dynamically. This is also possible in C using sprintf(). But here is a Perl demo:
    #!/usr/bin/perl use strict; use warnings; my $textlength=3; my $numlength=1; # on purpose too narrow my $fmt = "%-$textlength".'s '."%$numlength"."d\n"; print "format is:$fmt"; printf $fmt, "a",1; printf $fmt, "abc",123; printf $fmt, "abcdef",12345; __END__ prints: format is:%-3s %1d a 1 abc 123 abcdef 12345
    I like uniformity in the code and probably would have written the OP's code something like this:
    #!/usr/bin/perl use strict; use warnings; use List::Util qw(sum); my ($FreqP, $FreqN, $FreqZ) = map {int rand 41600} 0..2; my $Sum = sum($FreqP, $FreqN, $FreqZ); printf ( "Freq(Z+): %19d\n" , $FreqP) ; printf ( "Freq(Z-): %19d\n" , $FreqN) ; printf ( "Freq(0): %19d\n" , $FreqZ) ; printf ( "Total: %19d\n" , $Sum) ; __END__ Freq(Z+): 11640 Freq(Z-): 3527 Freq(0): 33243 Total: 48410
      my $fmt = "%-$textlength".'s '."%$numlength"."d\n";

      I don't see the point of all the dots. I think I would have used a straight double-quote interpolation:
          my $fmt = "%-${textlength}s %${numlength}d\n";
      It seems clearer to my eye IMHO.

      I like uniformity in the code ...

      I like it too, and also the idea of reducing code to data. If I were to go all the way with this, I might write something like this (which also takes care of a common colon that's running through all the strings):

      c:\@Work\Perl\monks>perl -wMstrict -le "use List::Util qw(sum); ;; my $Sum = sum my ($FreqP, $FreqN, $FreqZ) = map { int rand 41600 } 0. +.2; ;; my @rows = ( ['Freq(Z+)', $FreqP], ['Freq(Z-)', $FreqN], ['Freq(0)', $FreqZ], [' +Total', $Sum], ); ;; my $txtwidth = 9; my $numwidth = 16; ;; my $fmt = qq{%-*s %*d \n}; printf $fmt, $txtwidth, qq{$_->[0]:}, $numwidth, $_->[1] for @rows; " Freq(Z+): 25578 Freq(Z-): 39490 Freq(0): 13091 Total: 78159
      The  $fmt string can now be generated/stored/retrieved entirely independently, data to be printed are pure data, etc.

      But this is all probably overkill...


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

        my $fmt = "%-$textlength".'s '."%$numlength"."d\n";
        I don't see the point of all the dots.

        I did that to avoid the {} syntax which can confuse beginners.

        Your wrote: I think I would have used a straight double-quote interpolation:  my $fmt = "%-${textlength}s %${numlength}d\n"; It seems clearer to my eye IMHO. Yes, I agree IF the OP understands the curly bracket context here.