2006/12/28, Chad Perrin <[EMAIL PROTECTED]>:
On Thu, Dec 28, 2006 at 03:31:36AM -0500, M. Lewis wrote:
>
> Chad, I've been experimenting with this a bit since your posting. Maybe
> this will help: (I'm trying to understand the diff too)
>
> perl -le'
> my $ln = "one two three four ";
> print map " |$_| ", split /\s/, $ln;
> print map " |$_| ", split " ", $ln;
> '
> |one| || |two| || |three| || |four|
> |one| |two| |three| |four|
>
>
> perl -le'
> my $ln = "one two three four ";
> print map " |$_| ", split /\s+/, $ln;
> print map " |$_| ", split " ", $ln;
> '
> |one| |two| |three| |four|
> |one| |two| |three| |four|
That makes sense, considering I just checked the tutorials at PerlMonks
and discovered that, according to split(), ' ' and /\s+/ are exactly the
same. Frankly, I find that a bit surprising.
It is not the same:
[EMAIL PROTECTED] ~]$ perl -MData::Dumper -le "@foo = split /\s+/, qq( one two
three\t\n four ); print Dumper [EMAIL PROTECTED];"
$VAR1 = [
'',
'one',
'two',
'three',
'four'
];
[EMAIL PROTECTED] ~]$ perl -MData::Dumper -le "@foo = split ' ', qq( one two
three\t\n four ); print Dumper [EMAIL PROTECTED];"
$VAR1 = [
'one',
'two',
'three',
'four'
];
If you use /\s+/ and your string has trailing spaces at beginning, it will
create an empty string element on array, while using ' ' it won't.
HTH
--
Igor Sutton Lopes <[EMAIL PROTECTED]>