Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: compare 2 CSV files for string return lines in both if match

by kcott (Chancellor)
on Feb 03, 2018 at 10:23 UTC ( #1208390=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208390]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-11-16 20:35 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (200 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!