G'day davidinottawa,
I'd approach this in a number of different ways to what you've posted:
-
Your input file appears to be pipe-separated CSV.
Use Text::CSV:
that's already done most of the work for you.
-
Instead of attempting to hand-craft I/O error messages, use the autodie pragma.
Hand-crafting messages is error-prone: your first only says why, not what; your second only says what, not why.
-
Use the 3-argument form of open with lexical filehandles.
You did this for your first open;
however, the second uses a very generic package variable (i.e. INPUT) which could well be used elsewhere in your code (textually distant from the code you're looking at) and is therefore error-prone.
-
I'd use a hash for storing your match patterns.
Putting all that together (pm_1153098_match_csv_lines.pl):
#!/usr/bin/env perl -l
use strict;
use warnings;
use autodie;
use Text::CSV;
my $input_file = 'pm_1153098_match_csv_lines_input.csv';
my $pattern_file = 'pm_1153098_match_csv_lines_pattern.txt';
my %match_pattern;
open my $pat_fh, '<', $pattern_file;
while (<$pat_fh>) {
chomp;
++$match_pattern{$_};
}
close $pat_fh;
my $csv = Text::CSV::->new({sep_char => '|'});
open my $in_fh, '<', $input_file;
while (my $row = $csv->getline($in_fh)) {
print $row->[1] if $match_pattern{$row->[1]};
}
close $in_fh;
With these files:
$ cat pm_1153098_match_csv_lines_input.csv
David@domain.com|David@domain.com|J|ABBASS, DAVID JOHN|
Cory@domain.com|Cory@domain.com|E|ABBOTT, CORY J|
Tania@domain.com|Tania@domain.com|F|ABBOTT, TANIA LEE|
Geoffrey@domain.com|Geoffrey@domain.com|N|ABBOTT, GEOFFREY BRYAN|
$ cat pm_1153098_match_csv_lines_pattern.txt
Randall@domain.com
David@domain.com
Rob@domain.com
Tania@domain.com
I get this output:
$ pm_1153098_match_csv_lines.pl
David@domain.com
Tania@domain.com
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.