我正在使用List::Compare
来比较两个文件并打印出html中的输出,但由于我正在使用数组,因此输出会出现在一行而不是不同的行中。
例如
file1.txt
aaaa
bbbb
cccc
dddd
file2.txt
aaaa
bbbb
cccc
eeee
码
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;
以下是第一列的html输出:
<tr><td>aaaa
bbbb
cccc </td></tr>
我希望:
<tr><td>aaaa</td> <td>bbbb</td><td>cccc</td></tr>
我希望我已经妥善解释了。
答案 0 :(得分:2)
更改此行:
push(@body, "<tr><td>@intersection</td>\n");
为:
push @body, '<tr>', (map{'<td>'.$_.'</td>'}@intersection), '</tr>';
和其他数组@firstonly
和@secondonly
如果您想删除换行符,可以执行以下操作:
my @latest=<FILE1>;
chomp @latest;
my @pevious=<FILE2>;
chomp @pevious;
修改强>
根据您的评论,如果我理解,请尝试:
替换此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");
这一个:
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";
}
答案 1 :(得分:0)
此代码
push(@body, "<tr><td>@intersection</td>\n");
简单地将数组元素插入由空格分隔的字符串中。
你想要像
这样的东西 push(@body, "<tr>");
push(@body, map {"<td>$_</td>"} @intersection);
push(@body, "</tr>");