Hello, I'm trying to run a query and then from the results get the length of the longest value in a particular column and then use that number to pad the other columns so that all columns are equal widths... I know there are other ways to do this, but I'm (obviously) new and I'd like to know how to do it without any additional modules.

I've tried quite a few things (which have all failed for one reason or another), so to save space/everyone's time I'll just include the working code of the query:

use strict; use warnings; my $sth = $dbh->prepare($query); $sth->execute(); my ($TICKET_ID,$PRIORITY,$ABSTRACT); $sth->bind_columns(\($TICKET_ID,$ABSTRACT,$PRIORITY)); while (my $row = $sth->fetchrow_arrayref) { print "$TICKET_ID $ABSTRACT $PRIORITY \n"; }

So this returns something like:

TICKET123 Server unpingable Medium TICKET234 RPDU Audit Medium TICKET345 Network down High Ticket456 Check serial number Low Ticket567 Critical server unpingable High

So I'd like to get the length of the longest abstract, then pad the others so they're all the same width, making it look more like:

TICKET123 Server unpingable Medium TICKET234 RPDU audit Medium TICKET345 Network down High Ticket456 Check serial number Low Ticket567 Critical server unpingable High

Any help is greatly appreciated.


In reply to 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.