in reply to match a portion of a string from a file
Build a hash of keys keyed by cell number from the match file then run through the search file a line at a time looking for matches:
#!/bin/usr/perl use strict; use warnings; use Text::csv; my $csv_keys = <<CSV1; blue,lagoon,moon red,banana,sun CSV1 my $csv_phrases = <<CSV2; blue lagoon under a summer moon,still water,east of the sun hundreds of lines,several entries separated by commas,thanks in advanc +e CSV2 my %keys; my $csv = Text::CSV->new(); open my $keysIn, '<', \$csv_keys; while (my $row = $csv->getline($keysIn) ) { for my $cellNum (1 .. @$row) { $keys{$cellNum}{$row->[$cellNum - 1]} = $.; } } close $keysIn; open my $phrasesIn, '<', \$csv_phrases; while (my $row = $csv->getline($phrasesIn) ) { for my $cellNum (1 .. @$row) { for my $key (keys %{$keys{$cellNum}}) { next if $row->[$cellNum - 1] !~ /\Q$key\E/; print <<MATCH; Matched '$key' in cell $cellNum from keys line $keys{$cellNum}{$key} t +o phrases line $. MATCH } } } close $keysIn;
Prints:
Matched 'blue' in cell 1 from keys line 1 to phrases line 1 Matched 'sun' in cell 3 from keys line 2 to phrases line 1 Matched 'red' in cell 1 from keys line 2 to phrases line 2
It's assumed that the entire match string needs to match and that it should only be matched in the same "cell".
|
|---|