while (<INPUT>) {
($userName,$emailAddress,$division,$fullName) = split(/\|/, $_);
while (@lines) {
my $pattern = pop @lines;
$pattern=~s/\n//g;
if ($emailAddress =~ /$pattern/) {
print $pattern . " exists in " . $emailAddress."\n";
} else {
print $pattern . " no match " . $emailAddress."\n";
last;
}
}
should be
my $exists = 0;
while (<INPUT>) {
($userName,$emailAddress,$division,$fullName) = split(/\|/, $_);
while (@lines) {
my $pattern = pop @lines;
$pattern=~s/\n//g;
if ($emailAddress =~ /$pattern/) {
$exists = 1;
last;
}
}
if ($exists) {
print "$emailAddress exists\n";
} else {
print "$emailAddress doesn't exist"\n";
}
because you only want to print if it doesn't exist after you've checked all of the patterns, not just the first.
A hash would be make more sense, though.
sub main {
my $input_qfn = "input.file";
my $pattern_qfn = "pattern.file";
my %patterns;
{
open(my $pattern_fh, '<', $pattern_qfn)
or die("Can't open pattern file \"$pattern_qfn\": $!\n");
while (<$pattern_fh>) {
chomp;
++$patterns{$_};
}
}
open(my $input_fh, '<', $input_qfn)
or die("Can't open input file \"$input_qfn\": $!\n");
while (<$input_fh>) {
chomp;
my ($userName, $emailAddress, $division, $fullName) = split /\|/;
if ($patterns{$emailAddress}) {
print "$emailAddress exists in the pattern file\n";
} else {
print "$emailAddress doesn't exist in the pattern file\n";
}
}
}
Output:
David@domain.com exists in the pattern file
Cory@domain.com doesn't exist in the pattern file
Tania@domain.com exists in the pattern file
Geoffrey@domain.com doesn't exist in the pattern file
(Most people use four space indentations. One space is just not enough. You couldn't even tell that the indentations didn't line up correctly! As such, I doubled your indentation in my suggested solution.)