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

    If you use a decent module to *parse* CSV, why not also use a decent module to *produce* CSV?

    If Tie::Array::CSV, which uses mod::Text::CSV for parsing, has problems with blank lines, consider Spreadsheet::Read, which uses Text::CSV_XS. Both background parsers should have no problem with empty lines.


    Enjoy, Have FUN! H.Merijn
Re^2: Compare and group unmatched records from 2 CSV files together
by KIASohc (Novice) on Jun 24, 2014 at 11:27 UTC

    Thanks AppleFritter! I tried using the sample code but I am getting the error "CSV parse error: EIF - CR char inside unquoted, not part of EOL..."

    I tried looking up the error and found that this could be a data problem. I have recreated the same sample I gave and made sure that there are no extra characters/spaces but still getting the same result. Any idea why I am getting this?

      This smells like a binary \r is inside an unquoted field and the binary option is not passed to the parser. What versions of Text::CSV (and if installed Text::CSV_XS) and Tie::Array::CSV are you using?

      It might help to also show us a hextdump of the data that fails.


      Enjoy, Have FUN! H.Merijn
      Or this smells like a file prepared under Windows and used under Linux or Unix. If this is the case, just remove the CR (carriage returns, or \r) characters from your file before processing. One possible command to strip a file from these noisy Windows files under your shell:
      perl -pi.bak -e 's/\r//g;' my_file.txt
      This removes the Windows CR characters from the file and saves the original fila as my_file.txt.bak (just in case something goes wrong).

        And it will remove \r also when correctly quoted:

        code,value 1,"abc\rdef"

        Is valid CSV (binary => 1 needed for Text::CSV and Text::CSV_XS), but will change content with that change. Not good!


        Enjoy, Have FUN! H.Merijn