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; }
In reply to match a portion of a string from a file by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |