[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Newbee comments for macperl book



At 12.29 98.01.15, Brian L. Matthews wrote:
>|Here is an easier way:
>|$_ = <<'EOL';
>|   all text from here to that string above in single quotes goes into $_
>|   including the newlines.
>|   next line intentionally left blank
>|
>|   EOL #and this because its not on a line by itself
>|EOL
>|@lines = grep(!undef, split(/\n/,$_));
>|print join("\n", @lines),"\n";
>
>Not quite. I don't think split will ever produce an undefined array
>element. What it can produce is an empty string as an element, so:
>
>@lines = grep !/^$/, split /\n/;
>or
>@lines = grep $_ ne '', split /\n/;
>
>will split $_ into lines and remove any blank lines.
>
>Note that if you just want to remove *trailing* blank lines, split alone
>already does that.

Hrm.  Why did I put there?  The problem is that grep needs to test $_, and
undef doesn't test on $_.  *thwap*  But yes, split does return undefined
elements:

  @lines = grep($_ ne undef, split(/\n/,$_));

works just fine.  If you change ne to eq, you get one blank line.  :-)

Of course, maybe you should just move the file over to a Unix box instead
... *duck*

--
Chris Nandor               pudge@pobox.com           http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==                    MacPerl: Power and Ease                     ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch