in reply to Re^3: How to right align outputs of stored data in a variable?
in thread How to right align outputs of stored data in a variable?
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):
The $fmt string can now be generated/stored/retrieved entirely independently, data to be printed are pure data, etc.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
But this is all probably overkill...
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: How to right align outputs of stored data in a variable?
by Marshall (Canon) on Feb 07, 2017 at 20:53 UTC | |
by AnomalousMonk (Archbishop) on Feb 07, 2017 at 22:35 UTC |