从perl中的二维数组创建表有不同的输出

时间:2016-02-18 12:47:48

标签: html perl

您好我正在使用perl中的二维数组生成表格 但是如果在浏览器中查看并使用chrome中的开发人员工具查看页面源,我的程序输出会有所不同:

让我解释一下 -
我有一个子程序从@RESULT数组打印表,代码在

下面
sub printTableFormattedEmpty {
       my @array= @_ ;
            print "<table border='0' cellspacing='0' bgcolor='#cfcfcf' cellpadding='0'>\n";
            for(my $row_i = 0; $row_i < @array; $row_i++) {
                print "<tr style='background-color:#B39DB3;'>\n";

                for(my $column_i = 0; $column_i < @{ $array[$row_i] }; $column_i++) {
                    my $th = ($row_i == 0) ? "th" : "td";
                    print "</$th>";
                    print "$array[$row_i][$column_i]";
                    my $close = ($row_i == 0) ? 'th' : 'td';
                    print "</$close> \n";
                }

                print "</tr> \n";
            }
            print "</table> \n";
}

我正在调用子程序

{  
    print "Table starts here!\n";
      #$RESULT[0]- is array of many elements. u can see in output image
    $RESULT[1][0]= 'No Active bookings available for you !';
    $RESULT[2][0]= 'Click here to create new Booking !';
    &printTableFormattedEmpty(@RESULT);
}

现在我在这里没有得到预期的输出,我得到的输出不同,如图2所示

当我检查元素并检查表格时,我得到: enter image description here

但是当我查看页面的页面源时,我得到的输出格式为表格,如图所示: enter image description here

我对这两种类型的输出感到困惑,这两种图像都是同一页面而没有刷新 这怎么可能!
我的程序或其他内容是否有任何错误。

请帮帮我。

1 个答案:

答案 0 :(得分:3)

这是一个错字!

您的开始HTML代码输出中有一个斜杠/

for(my $column_i = 0; $column_i < @{ $array[$row_i] }; $column_i++) {
                    my $th = ($row_i == 0) ? "th" : "td";

                    #       V HERE
                    print "</$th>";
                    print "$array[$row_i][$column_i]";
                    my $close = ($row_i == 0) ? 'th' : 'td';
                    print "</$close> \n";
                }

删除斜杠,它会没问题。

至于为什么两个输出不同: HTML检查器在浏览器解析后显示DOM结构。它不包含无效元素。由于杂散关闭元素无效,因此解析器可能只是省略了它们,所以它们就消失了。

另一方面,查看源代码会显示真实的,未解析的代码,其中包含错误的标记,其中包含错误的HTML标记。这也是我看到额外斜线的地方。 (阅读:您的变量名称选择不当。如果有$open_tag$closing_tag,您会自己看到它。