in reply to Return rows from 2 csv files that don't match

As is oftne the case, the data structure you use makes a world of difference. In this case, I'd recommend a hash of the IP addresses found in file1. You could also store the actual line that triggered it for diagnostics, reporting, or debugging. This first example just records that file1 had the IP present.

my %hash; while ( my ( $ip, $other_stuff ) = parse_values_file_1() ) { $hash{ $ip } = 1; } while ( my ( $ip, $other_stuff ) = parse_values_file_2() ) { print "Not found: $ip ($other_stuff)\n" unless $hash{ $ip }; }

I'll leave the implementation of the parsing as an exercise for now. What you have here is a check for uniqueness. Whenever you think "uniqueness test" and "Perl", think "I can probably use a hash for that".

Replies are listed 'Best First'.
Re^2: Return rows from 2 csv files that don't match
by meredib (Acolyte) on Feb 19, 2009 at 18:59 UTC
    Thank you I figured it out. I didn't even think about using a hash but what you said is a good rule of thumb.
    #!perl use Text::CSV; $file1 = 'Master Sched.csv'; $file2 = 'Devices_01-03-2009-09-00-00.csv'; $csv = Text::CSV->new(); open IN1, "<"."$file1" or die "Can't open input file >$file1<\n"; while(<IN1>){ chomp; $csv->parse($_); @subIn = $csv->fields(); $agentName{$subIn[1]} = $_; $IP{$subIn[2]} = $_; $hostName{$subIn[3]} = $_; $product{$subIn[4]} = $_; foreach $x ( $agentName{$subIn[1]} ){ #print "$agentName{$subIn[1]}\n"; } } close IN1; open OUT, ">>"."$file1" or die "Can't open output file >$file1<\n"; print OUT "\n"; print OUT ",Differences\n"; open IN2, "<"."$file2" or die "Can't open input file >$file2<\n"; while ($in2 = <IN2>){ if($in2 =~ /Agent Name/){ next; } @subFlds = split(/,/, $in2); if($in2 =~ /\d+\.\d+\.\d+\.\d+/ || $subFlds[2] ne ''){ chomp $in2; $subFlds[1] =~ s/^\s+//; $subFlds[1] =~ s/\s+$//; $w = $agentName{$subFlds[0]}; $x = $IP{$subFlds[1]}; $y = $hostName{$subFlds[2]}; $z = $product{$subFlds[3]}; #print "$in2\n"; #print length($w)."w\n"; #print length($x)."x\n"; #print length($y)."y\n"; #print length($z)."z\n"; if(length($w) > 0 && length($x) > 0 && length($y) > 0){ $csv->parse($IP{$subFlds[1]}); @subFlds = $csv->fields(); #print "$in2\n"; }else{ print OUT ",$in2\n"; } } }