If you want to size the table according to maximum data widths then the * is worth knowing about in a printf format string:

use strict; use warnings; use List::Util qw(min max); my @names = qw(NameA NameB NameC NameD); my @ages = (353, 32, 2356, 75); my @sizes = (44, 212, 32, 328); my @scores = (900, 128, 99, 1000); my $nameWidth = max (map length, @names, 'name') + 3; my $ageWidth = max (map length, @sizes, 'Age') + 3; my $sizeWidth = max (map length, @sizes, 'Size') + 3; my $scoreWidth = max (map length, @scores, 'Score') + 3; print '-' x ($nameWidth + $ageWidth + $sizeWidth + $scoreWidth), "\n"; printf "%-*s%*s%*s%*s\n", $nameWidth, "Name", $ageWidth, "Age", $sizeWidth, "Size", $scoreWidth, "Score"; print '-' x ($nameWidth + $ageWidth + $sizeWidth + $scoreWidth), "\n"; for my $index (0 .. $#names) { printf "%-*s%*s%*s%*s\n", $nameWidth, $names[$index], $ageWidth, $ages[$index], $sizeWidth, $sizes[$index], $scoreWidth, $scores[$index]; }

Prints:

----------------------------- Name Age Size Score ----------------------------- NameA 353 44 900 NameB 32 212 128 NameC 2356 32 99 NameD 75 328 1000

DWIM is Perl's answer to Gödel

In reply to Re: Aligning Text by GrandFather
in thread Aligning Text by Dr.Avocado

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.