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.
In reply to Re: Compare and group unmatched records from 2 CSV files together
by AppleFritter
in thread Compare and group unmatched records from 2 CSV files together
by KIASohc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |