lisaw has asked for the wisdom of the Perl Monks concerning the following question:

Hello and Happy Holidays Perl Monks!
Quick question, I hope, I'm trying to figure out how to keep the Carriage Returns from being added after $one, which is an email address. Here's what I'm working with:
First the data is loaded into a textarea for editing...
open(FILE, "$datadir/$editlist\.txt") || &error_message("Can't find d +ata file - $datadir/$editlist\.txt."); @list = <FILE>; close(FILE); $numlist = @list; print "<textarea cols=60 rows=5 name='list'>\n"; $ccc = 0; for ($a = 0; $a < $numlist; $a ++) { ($two, $one, $nochop) = split(/,/, $list[$a]); if ($one =~ /.*\@.*\..*/) { print "$two,$one\n"; $ccc++; } } print <<"END"; </textarea><br>
Then the edited code is written to the db:
sub change_list { $list = $FORM{'list'}; $editlist = $FORM{'editlist'}; (@splitlist) = split(/\n/, $list); $numlist = @splitlist; open(FILE, ">$datadir/$editlist\.txt") || &error_message("Can't find +data file -$datadir/$editlist\.txt."); for ($a = 0; $a < $numlist; $a++) { ($two, $one) = split(/,/, $splitlist[$a]); $two =~ s/\cM//g; if ($one =~ /.*\@.*\..*/) { print FILE "$two,$one,x\n"; } } close(FILE);
Any suggestions to remove the CR's after the value $one?

Replies are listed 'Best First'.
Re: Remove Carriage Returns
by atcroft (Abbot) on Dec 18, 2002 at 23:10 UTC

    Try taking a look at chomp(). It may take care of what you are seeking.

Re: Remove Carriage Returns
by jdporter (Paladin) on Dec 19, 2002 at 03:16 UTC
    Any suggestions to remove the CR's after the value $one?
    Looks like that's been covered, but I have some other suggestions, if you're open to receiving them.

    1. Use lexical variables.
    2. Stick $! somewhere in your error messages for a failed concat().
    3. Iterate over the data lines using natural Perl idiom, instead of C idiom.
    4. Don't backwhack things that don't need it.
    Now, beyond that, it seems to me that you're doing the same operation in more than one place -- namely, filtering and reformatting valid data lines. This makes it a good candidate for a subroutine.

    sub valid_lines { map { my( $two, $one ) = split /,/; $one =~ /@.*\./ ? ( "$two,$one" ) : () } @_ } open FILE, "< $datadir/$editlist.txt" or error_message( "Can't read data file $datadir/$editlist.txt - +$!" ); my @lines = valid_lines( <FILE> ); close FILE; $ccc = @lines; local $, = "\n"; print <<EOF; <textarea cols="60" rows="5" name="list"> @lines </textarea><br/> EOF # Then the edited code is written to the db: sub change_list { my $editlist = $FORM{'editlist'}; $FORM{'list'} =~ s/\r//g; # kill useless carriage returns. open FILE, "> $datadir/$editlist.txt" or error_message( "Can't write data file $datadir/$editlist.t +xt - $!" ); for ( valid_lines( split /\n/, $FORM{'list'} ) ) { print FILE "$_,x\n"; } close FILE;
    Just an idea.

    jdporter
    ...porque es dificil estar guapo y blanco.

Re: Remove Carriage Returns
by lisaw (Beadle) on Dec 19, 2002 at 00:20 UTC
    Figured it out...

    Just needed to add a line to remove CR's and to remove any spaces in the value $one.
    As follows:
    $one =~ s/\s//g;
    Happy Holidays