Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks! I got some help a while back but need a little more. I have two CSV files and I'm trying to see if any data matches. I started by matching a line, got that to work, then got text in a line in a CSV file (separated by commas) to match but in every case in these files it's not a 1:1 match meaning I need to match a phrase like "blue" with something like "the blue water". My data sources have hundreds of lines and several entries separated by commas in each line. Please find the code below. I tried messing around with index on the match but couldn't get anything to work. Please find the code below and thanks in advance for the assistance!
#!/usr/bin/env perl use strict; use warnings; use autodie; use Text::CSV; my ($f1, $f2) = qw{1.csv 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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: match a portion of a string from a file
by GrandFather (Saint) on Feb 11, 2018 at 23:55 UTC | |
|
Re: match a portion of a string from a file
by choroba (Cardinal) on Feb 11, 2018 at 22:22 UTC | |
by Anonymous Monk on Feb 11, 2018 at 22:36 UTC | |
by choroba (Cardinal) on Feb 11, 2018 at 23:06 UTC | |
by AnomalousMonk (Archbishop) on Feb 12, 2018 at 00:49 UTC | |
|
Re: match a portion of a string from a file
by AnomalousMonk (Archbishop) on Feb 11, 2018 at 22:43 UTC | |
by poj (Abbot) on Feb 12, 2018 at 12:53 UTC |