http://www.perlmonks.org?node_id=1208390


in reply to compare 2 CSV files for string return lines in both if match

Jamming what should be blocks of code into a single line helps nobody, including yourself: it makes the code hard to read and is error-prone. Posting masses of irrelevant data, with records containing hundreds of characters, is not at all useful. Please read "How do I post a question effectively?" and "Short, Self-Contained, Correct Example".

Whenever you're dealing with CSV data, you should use Text::CSV. If you also have Text::CSV_XS installed, it will run faster.

It's very unclear what you're actually trying to achieve. The following is intended to provide you with some techniques that I think may be useful. You'll need to adapt this to your requirements.

Given these dummy CSV files:

$ cat pm_1208339_1.csv A,B,C D,E,F G,H,I A,D,G $ cat pm_1208339_2.csv B,C,D X,Y,X I,J,K

This script:

#!/usr/bin/env perl use strict; use warnings; use autodie; use Text::CSV; my ($f1, $f2) = qw{pm_1208339_1.csv pm_1208339_2.csv}; my %f1_values; my $csv = Text::CSV::->new; get_f1_data($f1, $csv, \%f1_values); parse_f2_data($f2, $csv, \%f1_values); sub get_f1_data { my ($file, $csv_obj, $f1_values) = @_; open my $fh, '<', $file; while (my $row = $csv_obj->getline($fh) ) { push @{$f1_values{$_}}, $row for @$row; } return; } sub parse_f2_data { my ($file, $csv_obj, $f1_values) = @_; open my $fh, '<', $file; while (my $row = $csv_obj->getline($fh) ) { my $matches = 0; print 'In line: '; $csv_obj->say(\*STDOUT, $row); for my $value (@$row) { next unless exists $f1_values->{$value}; ++$matches; print " $value found in:\n"; for my $line (@{$f1_values->{$value}}) { print ' '; $csv_obj->say(\*STDOUT, $line); } } print " No matches found\n" unless $matches; } return; }

Produces this output:

In line: B,C,D B found in: A,B,C C found in: A,B,C D found in: D,E,F A,D,G In line: X,Y,X No matches found In line: I,J,K I found in: G,H,I

That seems to be the type of thing you're after but, as I said, I'm really not sure. Hopefully there's something that you'll find useful. If you have any further questions, please follow the guidelines I linked to at the start.

— Ken