HTML表格格式问题

时间:2016-03-04 17:12:47

标签: html perl html-table two-columns

我试图以表格格式对齐两个数组的内容并发送电子邮件。但表中的第二列并未按需要对齐。第二列的内容显示为单行。

我也附加了输出表:

#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;
use HTML::Entities;

my $msg;
my @Sucess = qw(s1 s2 s3 s4 s5 s6);
my @Failed = qw(f1 f2 f3 f4);

my $html = '<table style="width:600px;margin:0 100px" border="1" BORDERCOLOR="#000000">
    <thead><th bgcolor="#9fc0fb">Successful</th><th bgcolor="#9fc0fb">Failed</th></thead>
    <tbody>';    

$html .= "<tr><td>$_</td>" for @Sucess;
$html .= "<td>$_</td>" for @Failed;
$html .= " </tr>";


$msg = MIME::Lite->new(
    from    => 'foo@abc.com',
    To      => 'foo@abc.com',
    Subject => 'Status of Update',
    Type    => 'multipart/related'
);

$msg->attach(
    Type => 'text/html',
    Data => qq{
        <body>
        <html>$html</html>
        </body>
    },
);

MIME::Lite->send ('smtp','xyz.global.abc.com' );
$msg->send;

Output Table

2 个答案:

答案 0 :(得分:2)

您需要使用按逻辑顺序工作的内容替换构建表的代码。需要逐行定义HTML表。你不能处理所有的成功,然后处理所有的失败。

我用这样的代码替换你的代码的中间部分:

use List::Util qw[max];

my $max = max($#Sucess, $#Failed);

for (0 .. $max) {
  $html .= '<tr><td>';
  $html .= $Sucess[$_] // '';
  $html .= '</td><td>';
  $html .= $Failed[$_] // '';
  $html .= "</td></tr>\n";
}

但实际上,我绝不会将原始HTML放在Perl程序中。请改用templating system

答案 1 :(得分:1)

首先,您遍历@Sucess中的每个项目以及每个项目:

  • 创建新行
  • 在该行中创建一个单元格
$html .= "<tr><td>$_</td>" for @Sucess;

然后,您查看@Failed中的每个项目以及每个项目:

  • 在您创建的最后一行(最近的@Sucess
  • 中创建一个单元格
$html .= "<td>$_</td>" for @Failed;

最后,您明确关闭您创建的最后一个表行:

$html .= " </tr>";

要获得所需的布局(明显非表格式),您需要逐行工作。您无法处理所有@Sucess的所有@Failed然后

my @Sucess= qw(s1 s2 s3 s4 s5 s6);
my @Failed= qw(f1 f2 f3 f4);

my $html = "<table>\n";

do {
    my $s = shift @Sucess;
    my $f = shift @Failed;
    $html .= sprintf("<tr> <td> %s </td> <td> %s </td> </tr> \n", map { $_ // '' } $s, $f);
} while ( @Sucess or @Failed );

$html .= "</table>";

print $html;