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".
|