Email Purge Again

P

perl

Ok, thanks for the helpful responses from my last post. I modified the
code with 2 different suggestions. I'm not using strict. Any ideas?

With this code.. %emails = map { chomp; $_, 1 } <$list>;
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 535, near "<$list>;"

With this code: $emails{lc $_} = 1 if ! exists $emails{lc $_};
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 534, near "1 if"

Here is the whole routine...
my $list2 = "$userpath/files/$list";
my %emails;
open(my $list, '<', $list2)
or error("Open failed $list2 $!");
while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$list>;
}

close($list) or warn $!;
 
T

Tim Greer

perl said:
Ok, thanks for the helpful responses from my last post. I modified the
code with 2 different suggestions. I'm not using strict. Any ideas?

With this code.. %emails = map { chomp; $_, 1 } <$list>;
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 535, near "<$list>;"

With this code: $emails{lc $_} = 1 if ! exists $emails{lc $_};
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 534, near "1 if"

Here is the whole routine...
my $list2 = "$userpath/files/$list";
my %emails;
open(my $list, '<', $list2)
or error("Open failed $list2 $!");
while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$list>;
}

close($list) or warn $!;

The above code shouldn't give that error. The code you've pasted above
must not be the exact code you're trying to run.
 
T

Tim Greer

Tim said:
The above code shouldn't give that error. The code you've pasted
above must not be the exact code you're trying to run.

Actually, looking again, you have $list in both the:

my $list2 = "$userpath/files/$list";

As well as for the $fh. Did you mean to use the same name for both (I
doubt it)? Also, what is the content of the file you're opening?
Perhaps try adding a check (which you'd want to do anything) in some
attempt to verify the value at least appears to be a valid email.
 
P

perl

Great! That fixed the error. BUT.. it's not eliminating duplicates...?
my $list2 = "$userpath/files/$list";
my %emails;
open(my $USERS, '<', $list2)
or error("Open failed $list2 $!");
while (<$USERS>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$USERS>;

}

close($USERS) or warn $!;
 
T

Tim Greer

perl said:
while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$list>;
}

Also, you don't need to put:

%emails = map {chomp; $_, 1 } <$list>;

in the while() loop -- it's just another method of doing the while/for.

In other words, either do:

while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
}

Or do:

%emails = map {chomp; lc, 1 } <$list>;

There are other ways, too.

Speaking of, you should do some check to see that the email appears to
be valid, and there are tools that can do the checking for you (so this
is an example, look for the right tool) and do something like:

next if !/^[-.\w]+\@[-\w.]+\.[a-z]{2,5}$/i;

in the while loop before $emails{lc $_}.

Or, for map, maybe something like:

%emails = map {
chomp;
(/^[-.\w]+\@[-\w.]+\.[a-z]{2,5}$/i)
? lc : '', 1
} <$list>;

Again, just quick examples based on the code you're trying to use above
(not necessarily the best method). Besides, both the original and
modified map methods will create an empty hash key, which you probably
don't want, so if you use that method, account for that to prevent it
from happening. The above is something I'd do (but that's just off the
top of my head for an example and some people may disagree with it), so
be sure to understand it instead of just throwing code into it, because
it might not be what you want or need (this is just based on your code
examples).
 
T

Tim Greer

perl said:
Great! That fixed the error. BUT.. it's not eliminating duplicates...?
my $list2 = "$userpath/files/$list";
my %emails;
open(my $USERS, '<', $list2)
or error("Open failed $list2 $!");
while (<$USERS>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$USERS>;

}

close($USERS) or warn $!;

How are you seeing the duplicates? That above should account for it,
unless perhaps you have something creating keys that are similar. That
hash should only have unique keys (it can't actually have duplicate key
names). How did you determine it's not eliminating them? By you
printing the hash keys? I assume not. Or by you re-checking the file
that should be updated with the now unique addresses? I'm thinking
it's how you're updating/writing to the file after the fact and an
issue with your code there.
 
T

Tim Greer

Tim said:
perl said:
while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$list>;
}

Also, you don't need to put:

%emails = map {chomp; $_, 1 } <$list>;

in the while() loop -- it's just another method of doing the
while/for.

In other words, either do:

while (<$list>) {
chomp if defined;
next if /^\s*$/;
$emails{lc $_} = 1 if ! exists $emails{lc $_};
}

Or do:

%emails = map {chomp; lc, 1 } <$list>;

There are other ways, too.

Speaking of, you should do some check to see that the email appears to
be valid, and there are tools that can do the checking for you (so
this is an example, look for the right tool) and do something like:

next if !/^[-.\w]+\@[-\w.]+\.[a-z]{2,5}$/i;

in the while loop before $emails{lc $_}.

Or, for map, maybe something like:

%emails = map {
chomp;
(/^[-.\w]+\@[-\w.]+\.[a-z]{2,5}$/i)
? lc : '', 1
} <$list>;

Again, just quick examples based on the code you're trying to use
above
(not necessarily the best method). Besides, both the original and
modified map methods will create an empty hash key, which you probably
don't want, so if you use that method, account for that to prevent it
from happening. The above is something I'd do (but that's just off
the top of my head for an example and some people may disagree with
it), so be sure to understand it instead of just throwing code into
it, because it might not be what you want or need (this is just based
on your code examples).

Hmm, my last post never showed up on my ISP's usenet server. I said
that you don't need to backwack @ in the regular expression (I was busy
with something else and wasn't thinking).
 
S

sln

Ok, thanks for the helpful responses from my last post. I modified the
code with 2 different suggestions. I'm not using strict. Any ideas?

With this code.. %emails = map { chomp; $_, 1 } <$list>;
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 535, near "<$list>;"

With this code: $emails{lc $_} = 1 if ! exists $emails{lc $_};
I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 534, near "1 if"

Here is the whole routine...
my $list2 = "$userpath/files/$list";
my %emails;
open(my $list, '<', $list2)
or error("Open failed $list2 $!");
while (<$list>) {
chomp if defined;
don't need, won't get here if not defined, plus why take off \n ?
next if /^\s*$/;
don't need, leaves everything past the first group of white spaces
$emails{lc $_} = 1 if ! exists $emails{lc $_};
don't need, why check for existence?
#%emails = map { chomp; $_, 1 } <$list>;
}

close($list) or warn $!;

If you trust your email lists for validity, the below
grabs just the email from the line and adds it as a key.
Dosen't matter if it already exists, just asigning it a value
of 1.

-sln

------------------------

use strict;
use warnings;

my %emails;

while (<DATA>) {
next if !(/^\s*(.*?)\s*$/ && length $1);
$emails{lc $1} = 1;
}
# display key/vals (but you only want the keys probably)
while( my ($key,$val) = each %emails) {
print "$key = $val\n";
}

__DATA__

aasdf

this is test
line 2
line 3
(e-mail address removed)
 
P

perl

Ok.. i guess it WAS working (big homer DOH!). i didn't post the rest
of the code because it didn't seem relevant. using bit's a pieces of
all the fine people that responded i figured it out. thanks to all.
below is finished code.
sub purge
{
my $list2 = "$userpath/files/$list";
my %emails;
open(my $USERS, '<', $list2)
or error("Open 1 failed $list2 $!");
while (<$USERS>) {
chomp if defined;
next if /^\s*$/;
# do a check here maybe and a "next" if not a valid email
#address.
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$USERS>;
}

close($USERS) or warn $!;

while( my ($key,$val) = each %emails) {
#print "$key = $val\n";
$newemails .= "$key\n";

}
#print "$_\n" for sort keys %emails;


open(my $USERS, '>', $list2)
or error("Open 2 failed $list2 $!");
print $USERS $newemails;
close($USERS) or warn $!;

&success("$list has been purged of duplicates");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top