Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Look for pattern in file. ????

by ikegami (Patriarch)
on Jan 19, 2016 at 17:07 UTC ( [id://1153101]=note: print w/replies, xml ) Need Help??


in reply to Look for pattern in file. ????

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.)

Replies are listed 'Best First'.
Re^2: Look for pattern in file. ????
by ikegami (Patriarch) on Jan 19, 2016 at 17:16 UTC
    Or maybe you want
    sub main { my $input_qfn = "input.file"; my $pattern_qfn = "pattern.file"; my %emailAddresses; { 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 /\|/ +; ++$emailAddresses{$emailAddress}; } } open(my $pattern_fh, '<', $pattern_qfn) or die("Can't open pattern file \"$pattern_qfn\": $!\n"); while (<$pattern_fh>) { chomp; if ($emailAddresses{$_}) { print "$_ exists in the input file\n"; } else { print "$_ doesn't exist in the input file\n"; } } }
    Randall@domain.com doesn't exist in the input file David@domain.com exists in the input file Rob@domain.com doesn't exist in the input file Tania@domain.com exists in the input file

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153101]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found