The reason you're not getting commas in your output is that you're stripping them from the lines you read from the files. Putting them back later on is impossible, for obvious reasons (no information on where they should be put is retained); you'll need different data structures to retain the, well, structure of your data.
That said, I'd suggest avoiding rolling your own CSV handling, and instead resorting to CPAN. Looking for useful modules there, I just found Tie::Array::CSV, and was able to use it thusly:
#!/usr/bin/perl
use feature qw(say);
use strict;
use warnings;
use Tie::Array::CSV;
tie my @file1, 'Tie::Array::CSV', 'FILE1.CSV';
tie my @file2, 'Tie::Array::CSV', 'FILE2.csv';
foreach my $row (0..$#file1) {
my @row1 = @{$file1[$row]};
my @row2 = @{$file2[$row]};
foreach my $col (0..$#row1) {
if($row1[$col] ne $row2[$col]) {
say "Row " . ($row + 1) . " - " . join ",", @row1;
say "Row " . ($row + 1) . " - " . join ",", @row2;
}
}
}
This produces the following output:
$ perl test.pl
Row 3 - RICK,SULLIVAN,31,MALE
Row 3 - RICK,SULLIVAN,30,MALE
Row 4 - SILVIA,CONOR,24,FEMALE
Row 4 - SILVIA,CONOR,24,MALE
$
BTW, Tie::CSV::Array seems to work best when there's no empty lines in your CSV files, so make sure they don't have 'em.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.