加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

数组 – 以可变宽度列打印数组内容

发布时间:2020-12-15 23:27:49 所属栏目:大数据 来源:网络整理
导读:我希望以这样的方式修改我的代码,使列扩展以容纳数据. 下面是一个断行的示例 +========+=========+===============+=============+=============+| Record | Cluster | Current Build | Current Use | Environment |+--------+---------+---------------+---
我希望以这样的方式修改我的代码,使列扩展以容纳数据.

下面是一个断行的示例

+========+=========+===============+=============+=============+
| 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 13).'+');
      print "n";
    }
   else {  
      if ($debug) {
         print "trouble with printData subroutinen";
      }
      return 0;
    }
}

解决方法

Text::Table将调整列宽以适合数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读