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

Re: [MacPerl] Newbee comments for macperl book



At 02.57 98.01.15, Fred&Thanh wrote:
>2 -  If you split the above string  on newlines the blank is an element in
>the array.You cannot use "\n" or  "" or even undef to parse out the empty
>element as below.  I think because of the dynamic linked list nature of
>arrays in perl. Now where did I read that?
>#continuing from above
>@lines = split("\n",$_);
>foreach (@lines) {    #$_ is target
>if("\n") { next; }     #wrong
>if("")    { next; }         #wrong
>if(undef) { next; }   #wrong
>if(!$_)     { next; }  #correct. $_ is logically false because linked list
>node has no string
>...
>}

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";


Get to know grep() and map() when you can.  They are your friends! :-)

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