CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Match or No Match, that is the question!!

 



English Guy
New User

Sep 2, 2002, 9:16 AM

Post #1 of 2 (3315 views)
Match or No Match, that is the question!! 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|");

while(<GREP>) {

if (/(\S+)\s?[-]\s?(\S+\s+\S+)/) { print "$1 - $2"; } #jsmith@dot.com - John Smith

else { print "No Match for $line"; }

}
}

close(GREP);

close(FILE);



Any Help appreciated!!!!


(This post was edited by English Guy on Sep 2, 2002, 9:16 AM)


davorg
Thaumaturge / Moderator

Sep 3, 2002, 2:10 AM

Post #2 of 2 (3306 views)
Re: [English Guy] Match or No Match, that is the question!! [In reply to] Can't Post


In Reply To
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 -

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


In Reply To

Code
  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".

In Reply To

Code
    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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives