Re: Comparing two CSV files
- From: usenet@xxxxxxxxxxxxxxx
- Date: 15 Mar 2006 12:14:36 -0800
Matt wrote:
I'm guessing I need to use nested loops.
Well, not like that. Your approach processes every line of file2
against every line of file1. But you question indicates that you only
want to process (say) line 14 of file2 against line14 of file1 (ie,
there is a one-to-one correspondance between the two files)
I'd use modules to simplify (and robust-ify) the parsing (this approach
assumes the CSV files are not huge and you don't mind holding them both
in memory):
#!/usr/bin/perl
use warnings; use strict;
use Text::CSV::Simple;
use List::MoreUtils qw{ each_array each_arrayref };
my $parser = Text::CSV::Simple->new;
my @csv1 = $parser->read_file('/tmp/csv1');
my @csv2 = $parser->read_file('/tmp/csv2');
my $each = each_array(@csv1, @csv2); #from List::MoreUtils
while ( my ($csv1, $csv2) = $each->() ) {
print join ",", @$csv1,
($$csv1[0] eq $$csv2[0] ? 'Not_' : '')
. "Modified\n";
}
__END__
--
http://DavidFilmer.com
.
- Follow-Ups:
- Re: Comparing two CSV files
- From: Matt
- Re: Comparing two CSV files
- References:
- Comparing two CSV files
- From: Matt
- Comparing two CSV files
- Prev by Date: svn library problem
- Next by Date: Re: svn library problem
- Previous by thread: Comparing two CSV files
- Next by thread: Re: Comparing two CSV files
- Index(es):