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

Re: [MacPerl] Merging elements



At 9:18 AM -0400 10/14/00, Ronald J Kimball wrote:

>The only difference between split(' ') and split(/\s+/) is that the former
>discards a leading null field while the latter does not.

...and of course, Ronald is correct, as I just now confirmed using 
the following script with MacPerl:

#!perl

$s = "  this is\ta\rtest\nof\fsplitting";
@a = split(' ', $s);
@b = split(/\s/, $s);

print $s;
print "\n---\n";
print join("*\n*", @a);
print "\n---\n";
print join("*\n*", @b);

__END__

I consider this an unfortunate state of affairs.  That is just IMHO 
of course, but that is the way I feel.  It is even the case that my 
well worn copy of "Programming Perl" documents this most clearly. 
Still, this is most irregular, and from my reading of "Programming 
Perl" is a holdover from the olden days when Perl was primarily seen 
as an alternative to awk.

Note that if you use:

split(/ /, $s);

This does what I would have wanted (e.g. only splits on spaces, not 
on tabs or anything else), so from now on I am going to be more 
careful to use just the // delimiter.

This thread started as a discussion of the best way to teach Perl, 
and I have to say that I would, myself, teach the // form, warning 
students about irregularities in Perl in general and irregularities 
in split in particular, and thus suggest they use delimiters other 
than // with caution.  I have fussed with this for about half an hour 
now, and reread the single page on split several times and I still am 
not confident I always can predict what split will do.

I recall very clearly my experience learning perl several years ago. 
I came to Perl with considerable experience with Fortran, Forth, and 
Pascal.  I was learning Perl 4 with the appropriate copy of 
"Programming Perl", and remember rereading the first example program 
about a billion times before I could wrap my head around all the 
special cases and shortcuts that Perl uses.  Perl may now be my 
favorite language (in close competition with Python), but I do think 
we need to be careful to appreciate how much trouble all the special 
cases and shortcuts can give to a new user.

-David-
David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org