This technique is adapted out of production C++ code that does something similar:

sub blank_formatter { my ($printf_format, $length) = @_; if (!defined($length)) { $printf_format =~ /^%(\d+)/ or die "Can't deduce length from $prin +tf_format"; $length = $1; } my ($blank) = ' ' x $length; sub { $_[0] ? sprintf($printf_format,$_[0]) : $blank; }; } sub my_printf { my ($format, @args) = @_; my (@printfargs) = (); while (@args) { my $arg = shift @args; if (ref($arg) and ref($arg) eq 'CODE') {push @printfargs, $arg->(s +hift @args);} else {push @printfargs, $arg;} } printf($format, @printfargs); } # similarly, if you need it, my_sprintf # Was: # printf( "A float: %12.4f$/", $root_beer ); my_printf( "A float: %s$/", blank_formatter('%12.4f'), $root_beer );

Then, if you need it, you can get whole aligned tables with something like:

my $fmt = blank_formatter('%12.4f'); my_printf("%10s: %s %s %s %s\n", $rowname, map {$fmt => $_} ($numcol1, $numcol2, $numcol3, $numcol4));

Similarly for other complicated formatting requirements. Just define your format generator so that it returns a sub reference that will take the thing to be formatted and returns a string, and then use printf as normal with %s placeholders.

-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re: Blankety-Blank Blanks and Zeros, etc. by fizbin
in thread Blankety-Blank Blanks and Zeros, etc. by rir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.