Email Purge

Discussion in 'Perl Misc' started by perl, Feb 24, 2009.

  1. perl

    perl Guest

    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");
    }
     
    perl, Feb 24, 2009
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Feb 24, 2009
    #2
    1. Advertising

  3. perl

    Tim Greer Guest

    perl wrote:

    > 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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Feb 24, 2009
    #3
  4. Gunnar Hjalmarsson <> wrote:
    > perl wrote:
    >> I have a routine intended to purge duplicate emails from a list.

    >
    > This OP posted to the beginners-cgi list, then to the beginners list.
    >
    > http://www.mail-archive.com//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 ).

    In:
    http://groups.google.com/groups/search?as_umsgid=

    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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 24, 2009
    #4
  5. Tim Greer wrote:
    >
    > while (<$list>) {
    > chomp; # maybe?
    > $emails{$_} = 1 if ! exists $emails{$_};
    > }


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

    I'd write:

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 24, 2009
    #5
  6. perl

    Tim Greer Guest

    Gunnar Hjalmarsson wrote:

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

    >
    > 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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Feb 25, 2009
    #6
  7. perl

    perl Guest

    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 $!;
     
    perl, Feb 25, 2009
    #7
  8. perl

    Jim Gibson Guest

    In article
    <>,
    perl <> wrote:

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

    > $emails{lc $_} = 1 if ! exists $emails{lc $_};
    > #%emails = map { chomp; $_, 1 } <$list>;
    > }
    >
    > close($list) or warn $!;


    --
    Jim Gibson
     
    Jim Gibson, Feb 25, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQ=?=

    Diable Explorer Button or Purge History List

    =?Utf-8?B?RGF2aWQ=?=, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    378
    =?Utf-8?B?RGF2aWQ=?=
    Feb 19, 2004
  2. purge like utility in c

    , Oct 10, 2006, in forum: C Programming
    Replies:
    118
    Views:
    1,666
    Dave Thompson
    Oct 23, 2006
  3. Signal

    File Read Cache - How to purge?

    Signal, Aug 21, 2007, in forum: Python
    Replies:
    14
    Views:
    502
    Hrvoje Niksic
    Aug 22, 2007
  4. how to purge backup files

    , May 10, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    189
    Joe Smith
    May 11, 2005
  5. perl

    Email Purge Again

    perl, Feb 25, 2009, in forum: Perl Misc
    Replies:
    9
    Views:
    163
Loading...

Share This Page