
davorg
Thaumaturge
/ Moderator
Sep 3, 2002, 2:10 AM
Post #2 of 2
(1185 views)
|
|
Re: [English Guy] Match or No Match, that is the question!!
[In reply to]
|
Can't Post
|
|
Hi, I have a simple script, which reads an email file, then greps another file which has the email address & real name of the users, then prints the email address & name if it finds a match, or prints No Match for $line if no match found. I cannot get it to wortk though!! My code is as follows - open(FILE, "$path/$file"); foreach $line(<FILE>) { open(GREP, "cat $path | grep $line|"); The line above is probably wrong. $path looks like it contains a directory name. And you don't want to pass a directory name to "cat" do you? If you checked the return status from "open" then Perl would tell you what's going wrong here.
while(<GREP>) { if (/(\S+)\s?[-]\s?(\S+\s+\S+)/) { print "$1 - $2"; } #jsmith@dot.com - John Smith What are you trying to do with the "[-]" expression. It's a over-complex way of writing "-". This complex regex can probably be written in a less complex fashion using "split".
else { print "No Match for $line"; } } } close(GREP); close(FILE); How big are your files? It might be more efficient to read all of the email addresses and names into a hash before you start. Also it's very inefficient to open a pipe to "grep" every time you go round your loop. -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
|