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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.