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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.