0

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.

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:

<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.

1
  • What's the first column? First it looked like you just get unchomped strings from List::Compare methods (which were interpolated in "<tr><td>@intersection</td></tr>" line), but then again, your last element is not followed by a newline... Commented Aug 22, 2012 at 17:05

2 Answers 2

2

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

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, ("&nbsp;")x($nbrows - @intersection);
push @firstonly, ("&nbsp;")x($nbrows - @firstonly);
push @secondonly, ("&nbsp;")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";
}
Sign up to request clarification or add additional context in comments.

2 Comments

hey thanks for your answer but it still comes in a one line. It should come in three different columns. REsult for "uniq in file1" should go under that column. Reuslt for "uniq in file2" should go under that particular column and so on.
hey thank you very much. this is exactly what i wanted. It was a great help. thanks again :)
0

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>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.