As a bit of practical advice, it is often not desirable to set the column widths to the "max" of the "longest text". A more common thing is like this:
#!/usr/bin/perl -w use strict; my ($ticket,$text,$priority) = ("Ticket567", "Critical server unpingable", "High"); printf "%-9s %-30s %s", $ticket,$text,$priority; # Ticket567 Critical server unpingable High
Use printf and a minimum field width specifier that is set to what works in the vast majority of cases. The "%-30s" means to reserve 30 spaces and left justify the text in that field. If this line's text has more than 30 characters, no data is lost (it will still print), but the columns after that one will be displaced.

Invariably somebody is going to type in the "Gettysburg Address" instead of a simple problem abstract. If every line uses that column's huge width, then the whole report is going to look screwy. If you use fixed widths that work "almost all the time", then when this happens just one line of the printout doesn't line up nicely - rather than having huge amounts of white space in all of the other lines to accommodate this one weirdo line.

Another thing that happens, is that often folks will want to cut and paste lines from different report days together into some new document. If the report column widths are consistent day to day, this is easier.

It is not my place to say that your requirement is "wrong" or that you don't need it. But you asked: "Any help is greatly appreciated.", I'm just presenting a plausible, practical alternative from the "real world".

Update: In printf "%-9s %-30s %s", this could have been written as printf "%-10s%-31s%s", but it is usually a mistake to rely upon the field width to account for the blank space between columns. By putting an explicit space in between fields in the format spec, when a column "overflows" because it is too wide to fit into the minimum space, this guarantees that there still be at least one space between the columns. If we were say printing integers and had "%3i %4i", that space ensures that we won't windup with something like "12345123456789" instead of "12345 123456789". This is a fine point, but understanding this will save you grief on some reporting project. Putting that explicit space in the format spec also makes it easier to read, so its a "win-win".


In reply to Re: Getting length of longest value in a column by Marshall
in thread Getting length of longest value in a column by Qukz

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.