[英]printing out proper html table using perl
I am using List::Compare
to compare two files and print out the output in html, but since I'm using arrays the output is coming out in one row instead of different rows. 我使用
List::Compare
比较两个文件并打印出html中的输出,但由于我使用的是数组,因此输出是在一行而不是在不同的行中输出。
example 例
file1.txt
aaaa
bbbb
cccc
dddd
file2.txt
aaaa
bbbb
cccc
eeee
code 码
use strict;
use warnings;
use Getopt::Long;
use List::Compare;
my $f1 = 'file1.txt';
open FILE1, "$f1" or die "Could not open file $f1 \n";
my $f2= 'file2.txt';
open FILE2, "$f2" or die "Could not open $f2 \n";
my $outputFile = 'finaloutput.txt';
my @body="";
push(@body, "<html> \n");
push(@body, "<head> \n");
push(@body, "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\" \n");
push(@body, " <TD>");
push(@body, "<div align=\"left\"><Table border=2 bordercolor= \"black\"> \n");
push(@body, "<tr bgcolor=\"ORANGE\"><TH><b>uniq in file1</b></TH><TH>uniq in file 2</TH><TH>common</TH></TR>");
push(@body, "<br>\n");
my @latest=<FILE1>;
my @pevious=<FILE2>;
my $compare = List::Compare->new(\@latest, \@pevious);
my @intersection = $compare->get_intersection;
my @firstonly = $compare->get_unique;
my @secondonly = $compare->get_complement;
print "Common in both:\n"."@intersection"."\n";
push(@body, "<tr><td>@intersection</td>\n");
print "uniq in first file:\n"."@firstonly"."\n";
push(@body, "<td>@firstonly</td>\n");
print "Items uniq in Second File:\n"."@secondonly"."\n";
push(@body, "<td>@secondonly</td></tr>\n");
push(@body, "</div></Table>" );
my $Joining= join('', @body);
push(@body, "</body></font>");
push(@body, "</html>");
print FILE"$Joining";
close FILE;
close FILE1;
close FILE2;
Here is the html output that I get for the first column: 这是我为第一列获得的html输出:
<tr><td>aaaa
bbbb
cccc </td></tr>
I want to have: 我希望有:
<tr><td>aaaa</td> <td>bbbb</td><td>cccc</td></tr>
I hope I have explained it properly. 我希望我已经妥善解释了。
Change this line: 改变这一行:
push(@body, "<tr><td>@intersection</td>\n");
to: 至:
push @body, '<tr>', (map{'<td>'.$_.'</td>'}@intersection), '</tr>';
and the same for other arrays @firstonly
and @secondonly
对于其他数组
@firstonly
和@secondonly
If you want to remove the line-feed, you can do: 如果要删除换行符,可以执行以下操作:
my @latest=<FILE1>;
chomp @latest;
my @pevious=<FILE2>;
chomp @pevious;
edit 编辑
According to your comment and if I well understand, try this: 根据你的评论,如果我很好理解,试试这个:
Replace this bock 替换这个博克
print "Common in both:\n"."@intersection"."\n";
push(@body, "<tr><td>@intersection</td>\n");
print "uniq in first file:\n"."@firstonly"."\n";
push(@body, "<td>@firstonly</td>\n");
print "Items uniq in Second File:\n"."@secondonly"."\n";
push(@body, "<td>@secondonly</td></tr>\n");
with this one: 这一个:
my $nbrows = @intersection;
$nbrows = @firstonly if @firstonly > $nbrows;
$nbrows = @secondonly if @secondonly > $nbrows;
push @intersection, (" ")x($nbrows - @intersection);
push @firstonly, (" ")x($nbrows - @firstonly);
push @secondonly, (" ")x($nbrows - @secondonly);
for my $i(0..$nbrows-1) {
push @body, "<tr>";
push @body, "<td>$firstonly[$i]</td>";
push @body, "<td>$secondonly[$i]</td>";
push @body, "<td>$intersection[$i]</td>";
push @body, "<tr>\n";
}
This code 这段代码
push(@body, "<tr><td>@intersection</td>\n");
simply interpolates the elements of the array into the string separated by spaces. 简单地将数组的元素插入由空格分隔的字符串中。
You want something like 你想要的东西
push(@body, "<tr>");
push(@body, map {"<td>$_</td>"} @intersection);
push(@body, "</tr>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.