Search code examples
perlstring-formatting

Drawing a table with horizontal lines and multiline cells


How do I display like in the following way in a table format?

Hostname     HostIPs     DomainName    nameservers    NSIps

My data is stored as follows for one row:

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

hostips, nsservers and nsips are arrays.

For one hostname - multiple hostIps and for one domain multiple nameservers and i have to display it in neatly formatted manner.

I tried using Text::Table, but it isn't displaying the way I want.

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

I have to display it as:

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   

Solution

  • Just include newlines into the cell values:

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