Chomp

Discussion in 'Perl Misc' started by zoomcart.com, Feb 20, 2009.

  1. zoomcart.com

    zoomcart.com Guest

    Hello and thanks in advance for you help.
    I have a simple email program that has a bulk add feature where a user
    can add an email list separated by returns. It works fine, except when
    the user tries to edit and remove an email, the script is not
    recognizing the email. If the email was added individually, it
    recognizes it. This may only be caused when the user is using a mac. I
    have tried several chomp variations.
    This is the bulk add code..
    sub add_bulk_email{
    my ($newemail, $bademail, $email, $newemail, $checkemail);
    $checkemail = param('checkemail');
    @emails = split(/\n+/, $checkemail);
    foreach $email(@emails){
    unless ($email =~ /.*\@.*\..*/) {
    $bademail=$email;
    }
    else{
    $emailcnt++;
    chomp $email;
    #chomp $email if ($email =~ /\n$/);
    #chomp $email if ($email =~ /\r$/);
    $newemail .= "$email\n";
    }
    }
    if($newemail){
    open (USERS, ">>$userpath/lists/$list") || &adminerror("$userpath/
    lists/$list Update Account" , __FILE__, __LINE__,);
    print USERS $newemail;
    close (USERS);
    &success("$emailcnt Bulk emails have been added to the list");
    }#new email
    else{ &adminerror("There were No REAL emails in your list");
    }
    ##########################
    This is the code for the individual add...
    sub add_new_email
    {
    my ($new_email, @fields, $checkemail, $line);
    $checkemail = param('checkemail');
    unless ($checkemail =~ /.*\@.*\..*/) {
    &adminerror("Please incude a REAL email address.");
    }
    if($checkemail eq ""){
    &adminerror("Please include an email address");
    }
    chomp $checkemail;
    if(-e "$userpath/lists/$list"){
    open (USERS, "$userpath/lists/$list") || &adminerror("$userpath/
    lists/$list add new email " , __FILE__, __LINE__,);
    flock(USERS, LOCK_EX);
    while (<USERS>)
    {
    $line = $_;
    chomp $line if ($line =~ /\n$/);
    if ($checkemail eq $line){
    $newemail = "";
    }
    else{
    $newemail = $checkemail;
    }#else
    } # End of while (<USERS>)
    close (USERS);
    }
    else{ &adminerror("$list was not found");}
    if($newemail){
    open (USERS, ">>$userpath/lists/$list") || &adminerror("$userpath/
    lists/$list Update Account" , __FILE__, __LINE__,);
    print USERS "$newemail\n";
    close (USERS);
    &success("$checkemail has been added to $list");
    }#new email
    else{ &adminerror("$checkemail is already on the list");
    }
    }#update
    #########################
    And this is the code for the remove email...
    sub adminremove_email
    my ($new_data, @fields, $old_data, $removeemail, $line);
    $removeemail = param('checkemail');
    if($removeemail eq ""){
    &adminerror("You must include an email");
    }
    else{
    $new_data = "";
    open (USERS, "$userpath/lists/$list") || &adminerror("$userpath/
    lists/$list Update" , __FILE__, __LINE__,);
    flock(USERS, LOCK_EX);
    while (<USERS>)
    {
    $line = $_;
    #chop $line;
    chomp $line;
    #chomp ($line);
    #chomp ($line) if ($line =~ /\n$/);
    #chomp $line if ($line =~ /\n$/);
    #chomp $line if ($line =~ /\r$/);
    if ($removeemail ne $line){
    $new_data .= "$line\n";
    }
    else{
    $old_data .= "$line\n";
    }#else
    } # End of while (<USERS>)
    close (USERS);
    if($old_data){
    open (USERS, ">$userpath/lists/$list") || &adminerror("$userpath/
    lists/$list Update Account" , __FILE__, __LINE__,);
    print USERS "$new_data";
    close (USERS);
    &success("$removeemail has been removed");
    }
    else{
    &adminerror("$removeemail was not found");
    }
    }#we have an email
    }#adminremove_email
     
    zoomcart.com, Feb 20, 2009
    #1
    1. Advertising

  2. "zoomcart.com" <> wrote in news:6e98e552-1d5e-
    :

    > Hello and thanks in advance for you help.
    > I have a simple email program that has a bulk add feature where a user
    > can add an email list separated by returns. It works fine, except when
    > the user tries to edit and remove an email, the script is not
    > recognizing the email. If the email was added individually, it
    > recognizes it. This may only be caused when the user is using a mac.


    Line endings are different on the Mac CRLF for DOSish systems, LF for
    *nixes and CR for the old Mac OS.

    The code you have provided below is simply an undreadable mess. Don't
    code like this but more importantly, don't post like this. Please take a
    look at the Posting Guidelines to learn how to help others help you.

    > while (<USERS>)
    > {
    > $line = $_;


    while ( my $line = <USERS> ) {

    > chomp $line if ($line =~ /\n$/);


    A line, by definition, ends with a newline.

    Assuming whitespace at the end of an email address cannot be significant
    (the assumption makes sense to me but I don't know the RFC), how about:

    $line =~ s/\s+$//;

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Feb 20, 2009
    #2
    1. Advertising

  3. zoomcart.com

    Tim Greer Guest

    zoomcart.com wrote:

    > Hello and thanks in advance for you help.
    > I have a simple email program that has a bulk add feature where a user
    > can add an email list separated by returns. It works fine, except when
    > the user tries to edit and remove an email, the script is not
    > recognizing the email. If the email was added individually, it
    > recognizes it. This may only be caused when the user is using a mac. I
    > have tried several chomp variations.
    > This is the bulk add code..
    > sub add_bulk_email{
    > my ($newemail, $bademail, $email, $newemail, $checkemail);
    > $checkemail = param('checkemail');
    > @emails = split(/\n+/, $checkemail);
    > foreach $email(@emails){
    > unless ($email =~ /.*\@.*\..*/) {


    As a couple of people have already covered a lot of problems with the
    code in other replies, I'll be short. The above check, just so you
    know, will match completely invalid addresses, including seeing "@." as
    a valid email address.

    > $emailcnt++;
    > chomp $email;
    > #chomp $email if ($email =~ /\n$/);
    > #chomp $email if ($email =~ /\r$/);


    Line feeds/new lines are not the same across all systems. Why not just
    strip out any whitespace/new lines altogether from the variable?
    --
    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 20, 2009
    #3
  4. zoomcart.com <> wrote:

    > @emails = split(/\n+/, $checkemail);



    That line of code looks familiar...

    .... it appears I posted it verbatim in a followup to you in April 2008.

    You're still working on the same program?


    > if($checkemail eq ""){
    > &adminerror("Please include an email address");
    > }



    I would prefer writing that as:

    adminerror("Please include an email address") unless length $checkemail;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 20, 2009
    #4
  5. zoomcart.com

    Guest

    On Fri, 20 Feb 2009 10:43:53 -0800 (PST), "zoomcart.com" <> wrote:

    [snip]
    I started off with good intentions to read this code,
    but stopped after gazing down at it.
    Maybe I will read it if its cleaned up with big indentation,
    and uncommented with each line of code as a comment.

    -sln
     
    , Feb 21, 2009
    #5
    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. Aaron Powell
    Replies:
    0
    Views:
    1,224
    Aaron Powell
    Jul 24, 2003
  2. Matt Taylor

    Question on Chomp

    Matt Taylor, Sep 29, 2003, in forum: Perl
    Replies:
    2
    Views:
    391
    Matt Taylor
    Sep 29, 2003
  3. Fernando Armenta

    How you chomp in python

    Fernando Armenta, Sep 20, 2003, in forum: Python
    Replies:
    3
    Views:
    8,617
  4. Jeremy Dillworth

    Re: How you chomp in python

    Jeremy Dillworth, Sep 20, 2003, in forum: Python
    Replies:
    0
    Views:
    983
    Jeremy Dillworth
    Sep 20, 2003
  5. martin
    Replies:
    3
    Views:
    202
    Joe Smith
    Apr 15, 2006
Loading...

Share This Page