Email Purge

P

perl

Cheers and thanks in advance for you help. I have a routine intended
to purge duplicate emails from a list. The code below is just. I'm
sure it needs to be in a %hash but I'm lost. Any help is appreciated.
sub purge
{
open (LIST, "$list") or error("$list email purge 1");
while (my $line = <LIST>)
{
@emails = split(/\r?\n|\r/, $line);
@emails2 = @emails;
}
close (LIST);
foreach $email(@emails)
{

foreach $email2(@emails2)
{
if($email ne $email2)
{ $newemail .="$email\n"; }
else{ $purgecnt++; }
}
}
open (LIST, ">$list") or error("$list email purge 2");
flock(LIST, LOCK_EX);
print LIST $newemail;
close (LIST);
&success("$list has been purged of $purgecnt duplicates");
}
 
T

Tim Greer

perl said:
Cheers and thanks in advance for you help. I have a routine intended
to purge duplicate emails from a list. The code below is just. I'm
sure it needs to be in a %hash but I'm lost. Any help is appreciated.
sub purge
{
open (LIST, "$list") or error("$list email purge 1");

Maybe use the preferred method and use $! to tell you why it failed:

open(my $filelist, '<', $list) or error ("Can't open $list $!");
while (my $line = <LIST>)
{
@emails = split(/\r?\n|\r/, $line);
@emails2 = @emails;

Why not just use @emails? Why use the same array twice?

<snip>

Nevermind, I see what you're trying to do.

Yes, use a hash to see if the address already exists.

my %emails = ();
open(my $list, '<', 'filename.here')
or error("Open failed for filename.here $!");
while (<$list>) {
chomp; # maybe?
$emails{$_} = 1 if ! exists $emails{$_};
}
close($list) or warn $!;

Now, %emails contains (hopefully unique) email addresses.

I assume emails are one per line, which means you step through each
line, which you were doing already with "while" (so trying to split on
new lines is redundant).

The above is a very basic example, though I'd also suggest ignoring the
case on matches, so I'd set $_ to lc and compare against the existing
key in lc (lower case) as well. That's about as simple as it can get.

PS: The above is untested, I literally have 60 seconds from the start of
this to heading out of the office, so it's an example only just off the
top of my head.
 
T

Tad J McClellan

Gunnar Hjalmarsson said:
This OP posted to the beginners-cgi list, then to the beginners list.

http://www.mail-archive.com/[email protected]/msg99536.html

He does not respond to the comments and suggestions, and should
consequently be ignored.


I've had him killfiled for 5 years, but he changed his posting address
this time (it used to be (e-mail address removed)).

In:
http://groups.google.com/groups/[email protected]

he said:

Thanks in advance for any help.
(please NO rectal discharge)

Which I took as a big honking red flag that his posts could be ignored.
 
G

Gunnar Hjalmarsson

Tim said:
while (<$list>) {
chomp; # maybe?
$emails{$_} = 1 if ! exists $emails{$_};
}

Why that condition? It's a hash. ;-)

I'd write:

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

Tim Greer

Gunnar said:
Why that condition? It's a hash. ;-)

I'd write:

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

I was just going with the same basic style the user originally posted,
which should (in theory) help them better understand (instead of
explaining map or something else to them. I'd do a lot of things
differently, too.
 
P

perl

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

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

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

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*$/;
# 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 } <$list>;
}

close($list) or warn $!;
 
J

Jim Gibson

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

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

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

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*$/;
# do a check here maybe and a "next" if not a valid email
address.

You need to put a '#' character at the beginning of the previous line.
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top