4

我希望修改我的代码,使列变宽以容纳数据。

下面是一个断行的例子

+========+=========+===============+=============+=============+
| Record | Cluster | Current Build | Current Use | Environment |
+--------+---------+---------------+-------------+-------------+
| 3      | 1       | v44           | v44 Live (currently - new company cluster)| PROD        |
+--------+---------+---------------+-------------+-------------+

这是我正在使用的(kludgy)代码

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->{'Record ID#'});
         printf ("%-10s",'| '.$rows->{'Cluster'});
         printf ("%-16s",'| '.$rows->{'Current Build'});
         printf ("%-14s",'| '.$rows->{'Current Use'});

            # calculate the length of the last column
            my $length = length($rows->{'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->{'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;
    }
}
4

4 回答 4

9

Text::Table将调整列宽以适应数据。

于 2011-08-25T23:23:20.787 回答
3

您必须预先扫描数据以获得每个字段的最大宽度,然后构建您的格式字符串。

于 2011-08-25T21:39:42.197 回答
2

Text::SimpleTable::AutoWidth非常简单,比 Text::Table 简单。

于 2011-08-25T23:49:17.367 回答
2

我对已经建议的任何一个模块都不熟悉,所以如果它们不能按您的意愿工作,那么我会按照 Mouse Food 的建议进行操作,并预先扫描数据以获得每列的最大宽度。然后在输出时将这些最大宽度用于格式字符串。如果可能的话,在构建数组时获得这个最大值可能会更有效,而不是在最后迭代两次。

下面的代码遍历整个数组以找到最大列长度,但如果您在进行时计算它,它应该很容易适应。此外,我已经立即写了这个并且没有对其进行测试,但它应该给你这个想法。

my %maximums = {
    "Record ID#" => 0,
    "Cluster" => 0,
    "Current Build" => 0,
    "Current Use" => 0
};

# calculate the maximum length of the values in each column
foreach my $row (@_) {
    foreach my $col (keys %maximums) {
        my $col_length = length($row->{$col});
        $maximums{key} = $col_length if ($col_length > $maximums{$col});
    }
}

# Generate a format string using the maximum column widths.  Other format strings
# may be generated in this loop for the other parts of the table.  Alternatively
# you could probably transform the one string and replace the characters in it.
my $row_format = "|";
foreach my $col (keys %maximums) {
    $row_format .= " %-",$maximums{$key},"s |";
}
$row_format .= "\n";

# Print your headers and borders here, using the other format strings that you
# would calculate above (where $row_format is generated).

# Print each row in the table
foreach my $row (@_) {
    printf($row_format, $row->{"Record ID#"}, $row->{"Cluster"},
        $row->{"Current Build"}, $row->{"Current Use"});
}
于 2011-08-26T03:01:48.493 回答