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: Beginner:
Why this wont work

 



tuxtutorials
Novice

Jun 14, 2011, 5:56 PM

Post #1 of 7 (819 views)
Why this wont work Can't Post

Hi Guys, can anyone here give insight onto why the following code won't work? I am basically trying to compare two files, that contain email addresses: Ex: someone@domain.com

The goal is to read both files into arrays then match the domains that are in one file to another and print them out, but doesn't appear to be working:


Code
#!/usr/bin/perl 

open USERS, "<out.txt";
open GMS, "<gms.txt";

my @users = <USERS>;
my @gms = <GMS>;


foreach $user (@users)
{
if (/$user/ =~ @gms) {
print $_;
}
else {
print "not matched\n";
}
}


I know its the most retarded code ever and there probably is a better way to do this.


miller
User

Jun 14, 2011, 6:10 PM

Post #2 of 7 (817 views)
Re: [tuxtutorials] Why this wont work [In reply to] Can't Post

Try this for matching:


Code
#!/usr/bin/perl 

use strict;
use warnings;

my @users = qw(foo bar baz);
my @gms = qw(notFoo bar notBaz);

foreach $user (@users) {
if ($user ~~ @gms) {
print "$user matched\n";
} else {
print "$user not matched\n";
}
}


- Miller


tuxtutorials
Novice

Jun 14, 2011, 8:54 PM

Post #3 of 7 (810 views)
Re: [miller] Why this wont work [In reply to] Can't Post

Thanks Miller for response however, I don't think that was the issue. I am thinking it is something with the @email addresses I have in the file. I replaced the samples in your code with:


Code
#!/usr/bin/perl  

my @users = ("foo@mail.com","bar@mail.com","baz@mail.com");
my @gms = ("notFoo@mail.com","bar@mail.com","notBaz@mail.com");

foreach $user (@users) {
if ($user ~~ @gms) {
print "$user matched\n";
} else {
print "$user not matched\n";
}
}


When I run it I get the following output:

foo.com not matched
bar.com not matched
baz.com not matched

Looks like the @ is causing the problem so I went back in to the code and edited the following:




Code
 
#!/usr/bin/perl

my @users = ("foo@mail.com","bar@mail.com","baz@mail.com");
my @gms = ("notFoo@mail.com","bar@mail.com","notBaz@mail.com");

foreach $user (@users) {
if ('$user' ~~ @gms) {
print "$user matched\n";
} else {
print "$user not matched\n";
}
}


Thinking that if I used single quotes around each item in the array it would keep the @ sign from being interpreted but no go. Any other suggestions?

My: out.txt file basically looks like this:

foo@mail.com
bar@mail.com
baz@mail.com

and my gms.txt file looks like this:

notFoo@mail.com
bar@mail.com
notBaz@mail.com

My goal was to iterate through each line from out.txt and compare to gms.txt. If anything matched I would print to stdout and the stuff that didn't match would print "not matched" This way I could get a list of email addresses that matched and those that didn't.


(This post was edited by tuxtutorials on Jun 14, 2011, 8:55 PM)


miller
User

Jun 15, 2011, 10:37 AM

Post #4 of 7 (800 views)
Re: [tuxtutorials] Why this wont work [In reply to] Can't Post


In Reply To
Thanks Miller for response however, I don't think that was the issue. I am thinking it is something with the @email addresses I have in the file. I replaced the samples in your code with:


Code
#!/usr/bin/perl  

my @users = ("foo@mail.com","bar@mail.com","baz@mail.com");
my @gms = ("notFoo@mail.com","bar@mail.com","notBaz@mail.com");

foreach $user (@users) {
if ($user ~~ @gms) {
print "$user matched\n";
} else {
print "$user not matched\n";
}
}



No, you simply introduced a NEW problem by not using single quoted strings in your fake data.

Either use qw() like I originally demonstrated:


Code
my @users = qw(foo@mail.com bar@mail.com baz@mail.com); 
my @gms = qw(notFoo@mail.com bar@mail.com notBaz@mail.com);


Or use single quoted strings


Code
my @users = ('foo@mail.com','bar@mail.com','baz@mail.com');  
my @gms = ('notFoo@mail.com','bar@mail.com','notBaz@mail.com');


Or just escape the @ in the double quoted string so it isn't interpolated as an array:


Code
my @users = ("foo\@mail.com","bar\@mail.com","baz\@mail.com");  
my @gms = ("notFoo\@mail.com","bar\@mail.com","notBaz\@mail.com");


Whatever you choose, this won't be an issue when you're pulling raw data from a file.

- Miller


tuxtutorials
Novice

Jun 15, 2011, 8:17 PM

Post #5 of 7 (791 views)
Re: [miller] Why this wont work [In reply to] Can't Post

Thanks miller, it works perfect now I was able to iterate through my 4k+ email list. One thing still on my mind is

1. What is the diff between =~ and ~~?

Thanks


miller
User

Jun 16, 2011, 3:40 PM

Post #6 of 7 (786 views)
Re: [tuxtutorials] Why this wont work [In reply to] Can't Post

~~ is the newish smart matching operator. Read about it here:

http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

- Miller


tuxtutorials
Novice

Jun 16, 2011, 8:05 PM

Post #7 of 7 (779 views)
Re: [miller] Why this wont work [In reply to] Can't Post

Thanks again miller.

 
 


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

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