in reply to Compare and group unmatched records from 2 CSV files together
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Compare and group unmatched records from 2 CSV files together
by Tux (Canon) on Jun 24, 2014 at 10:23 UTC | |
|
Re^2: Compare and group unmatched records from 2 CSV files together
by KIASohc (Novice) on Jun 24, 2014 at 11:27 UTC | |
by Tux (Canon) on Jun 24, 2014 at 11:39 UTC | |
by Laurent_R (Canon) on Jun 24, 2014 at 22:01 UTC | |
by AppleFritter (Vicar) on Jun 24, 2014 at 22:17 UTC | |
by Laurent_R (Canon) on Jun 25, 2014 at 06:00 UTC | |
by Tux (Canon) on Jun 25, 2014 at 12:09 UTC | |
by Laurent_R (Canon) on Jun 25, 2014 at 17:20 UTC |