Bryan Harris wrote:
> 
> Wow, Johns, thanks for the tips...  I'm going to have to study that map
> command very closely.  Either way you've answered my question, thanks!
> 
> Only one thing to add--  you ask:
> 
> >>    chomp $newtxt;
> >>    print $newtxt, "\n";
> >
> > Why remove "\n" in one line and then add it back on the next line?
> 
> I did this because I want to make sure I end with a "\n", but I don't want
> an extra one if one is already there.  I guess I could've also done a:
> 
> $newtxt =~ s/([^\n])$/$1\n/;

The character class ([^\n]) can also be written as (.)

> ... but the above seemed clearer.  Is that not a good reason?

The reason is good but the regular expression is flawed because:

perldoc perlre
[snip]
           $   Match the end of the line (or before newline at the end)


Here is a test program showing what happens:

$ perl -le'
@x = ( "zero", "one\n", "two\n\n", "three\n\n\n" );
for ( @x ) {
    $x = tr/\n//;
    print "$x: *>$_<*";
    s/([^\n])$/$1\n/;
    $x = tr/\n//;
    print "$x: *>$_<*";
    }'
0: *>zero<*
1: *>zero
<*
1: *>one
<*
2: *>one

<*
2: *>two

<*
2: *>two

<*
3: *>three


<*
3: *>three


<*


You probably want to do this instead:

$newtxt =~ s/\n*\z/\n/;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to