perl 绘制具有水平线和多行单元格的表格

jqjz2hbq  于 2023-05-29  发布在  Perl
关注(0)|答案(3)|浏览(161)

如何以表格形式显示如下内容?

Hostname     HostIPs     DomainName    nameservers    NSIps

我的数据存储如下一行:

$hostName, @hostIps, $domainName, @nservers, @nsips

hostip、nsservers和nsip是阵列。
对于一个主机名-多个hostIps和一个域多个名称服务器,我必须以整齐的格式显示它。
我尝试使用Text::Table,但它并没有显示我想要的方式。

my $tb = Text::Table->new("hostName", "IP Address", "domainName", "nameServers", "IP  addr");
$tb->add ($hostName, @hostIps, $domainName, @nservers, @nsips);
print $tb;

我必须将其显示为:

hostName IP Address domainName nameServers IP  addr
host1    ip1.1        domain1    serv1.1       addr1.1   
         ip1.2                   serv1.2       addr1.2  
         ip1.3                   serv1.3       addr1.3
host2    ip2        domain2    serv2       addr2 
          ..                      ..       ..
host3    ip3        domain3    serv3       addr3
gg0vcinb

gg0vcinb1#

只需在单元格值中包含换行符:

$tb->add($hostName,
         join("\n", @hostIps),
         $domainName,
         join("\n", @nservers),
         join "\n", @nsips
        );
xggvc2p6

xggvc2p63#

对于Text::Table,需要在循环中add行:

use warnings;
use strict;
use Text::Table;

my $tb = Text::Table->new("hostName", "IP Address", "domainName", "nameServers", "IP  addr");
for (1 .. 3) {
    $tb->add("host$_", "ip$_", "domain$_", "serv$_", "addr$_");
}
print $tb;

__END__

hostName IP Address domainName nameServers IP  addr
host1    ip1        domain1    serv1       addr1   
host2    ip2        domain2    serv2       addr2   
host3    ip3        domain3    serv3       addr3

相关问题