Mixing tabs and spaces has always been a recipé for disaster, but if you are prepared to settle for spaces throughout, then the simplest technique is to use a format code processor (here I used LEFT CENTRE and LM4 for the more weird one) which requires only a tiny piece of code each when applied with the minor trick of overwriting a preformatted template field of just spaces.
#!/usr/bin/perl # (tested) use strict; use warnings; my @AoA = (); Insert( \@AoA, 'Planet', 'Radius', 'Density' ); Insert( \@AoA, 'Mercury', 2360, 3.7 ); Insert( \@AoA, 'Jupiter', 71030, 1.3 ); print Formit( \@AoA ); sub Formit { my $aoaref = shift; my $aref = $aoaref -> [0]; my $return = Line($aref); $return .= FormatLine($aref, 'CENTRE', 'CENTRE', 'CENTRE' ); $return .= Line($aref); shift @$aoaref; for $aref ( @$aoaref ) { $return .= FormatLine( $aref, 'LEFT', 'LM4', 'LM4' ); } $return .= Line( $aref ); return $return; } sub FormatLine{ my $aref = shift; my $return = '|'; for my $field ( @$aref ) { my $format = shift; my $formatted = ( ' ' x 12 ); my $pos = ( $format eq 'LEFT' ) ? 0 : ( $format eq 'CENTRE' ) ? 6 - (length( $field ) / 2 +) : ( $format =~ /^LM(\d+)/ ) ? $1 : 0; substr( $formatted, $pos, length( $field ) ) = $field; $return .= $formatted . '|'; } return $return . "\n"; } sub Line { my $aref = shift; my $cols = ( 13 * ( $#$aref + 1 ) ) + 1; return ( '-' x $cols ) . "\n"; } sub Insert{ my $aoaref = shift; my @a = @_; push @$aoaref, \@a; }

-M

Free your mind


In reply to Re: Printing Issues with Text::Table by Moron
in thread Printing Issues with Text::Table by neversaint

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.