Email Purge Again

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

  1. perl

    perl Guest

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

  2. perl

    Tim Greer Guest

    perl wrote:

    > 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.
    --
    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
    #2
    1. Advertising

  3. perl

    Tim Greer Guest

    Tim Greer wrote:

    > perl wrote:
    >
    >> 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.


    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.
    --
    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
    #3
  4. perl

    perl Guest

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

    Tim Greer Guest

    perl wrote:

    > 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).
    --
    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
    #5
  6. perl

    Tim Greer Guest

    perl wrote:

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

    Tim Greer Guest

    Tim Greer wrote:

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

    ^^^
    And, sorry, you don't need to deference/backwack for @ in the regex.
    --
    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
    #7
  8. perl

    Tim Greer Guest

    Tim Greer wrote:

    > perl wrote:
    >
    >> 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).
    --
    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
    #8
  9. perl

    Guest

    On Wed, 25 Feb 2009 14:20:48 -0800 (PST), perl <> wrote:

    >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
     
    , Feb 26, 2009
    #9
  10. perl

    perl Guest

    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");
    }
     
    perl, Feb 26, 2009
    #10
    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:
    389
    =?Utf-8?B?RGF2aWQ=?=
    Feb 19, 2004
  2. purge like utility in c

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

    File Read Cache - How to purge?

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

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

    Email Purge

    perl, Feb 24, 2009, in forum: Perl Misc
    Replies:
    7
    Views:
    151
    Jim Gibson
    Feb 25, 2009
Loading...

Share This Page