title: print array contents in variable width columns
body: <p>I wish to modify my code in such a way that the column widens to accommodate the data. </p>
  
  <p>Below is an example of a broken row </p>
  
  <pre><code>+========+=========+===============+=============+=============+
  | Record | Cluster | Current Build | Current Use | Environment |
  +--------+---------+---------------+-------------+-------------+
  | 3      | 1       | v44           | v44 Live (currently - new company cluster)| PROD        |
  +--------+---------+---------------+-------------+-------------+
  </code></pre>
  
  <p>Here is the (kludgy) code I'm using </p>
  
  <pre><code>sub printData {
     if (@_) {
        # print the data grid top border
        printf ("%10s%10s%15s%14s%14s",'+'.('=' x 8).'+',('=' x 9).'+',('=' x 15).'+',('=' x 13).'+',('=' x 13).'+');
        print "\n";
        # print the data grid column titles
        printf ("%-9s%-10s%-16s%-14s%-15s",'| Record ','| Cluster ','| Current Build ','| Current Use ','| Environment |');
        print "\n";
  
        # print out each row of data
        foreach my $rows (@_) {
  
           # print the data grid demarcation border
           printf ("%10s%10s%15s%14s%14s",'+'.('-' x 8).'+',('-' x 9).'+',('-' x 15).'+',('-' x 13).'+',('-' x 13).'+');
           print "\n";
  
           # print each data cell
           printf ("%-9s",'| '.$rows-&gt;{'Record ID#'});
           printf ("%-10s",'| '.$rows-&gt;{'Cluster'});
           printf ("%-16s",'| '.$rows-&gt;{'Current Build'});
           printf ("%-14s",'| '.$rows-&gt;{'Current Use'});
  
              # calculate the length of the last column
              my $length = length($rows-&gt;{'Environment'});
  
              # calculate how many spaces to add to the last column
              # the title of this column uses 15 characters (including '|' and spaces)
              # we already used three of those spaces for 2 '|' characters  and 1 leading space
              # so 15 - 3 = 12 
              # then subtract the length of the return string from 12
              my $spaces = 12 - $length;
  
           # we print the last data cell plus the padding spaces calculated above
           printf ("%-15s",'| '.$rows-&gt;{'Environment'}.(' ' x $spaces).'|');
           print "\n";
        }
  
        # we print the bottom data grid border
        printf ("%10s%10s%15s%14s%14s",'+'.('=' x 8).'+',('=' x 9).'+',('=' x 15).'+',('=' x 13).'+',('=' x 13).'+');
        print "\n";
      }
     else {  
        if ($debug) {
           print "trouble with printData subroutine\n";
        }
        return 0;
      }
  }
  </code></pre>