Igor Sutton wrote:
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


Very interesting and informative Igor. Thanks. More experimentation needed to get this clear in my head.

Thanks,
Mike

--

 IBM: Inadequates Becoming Millionaires
  04:10:01 up 14 days,  1:01,  0 users,  load average: 0.30, 0.76, 0.61

 Linux Registered User #241685  http://counter.li.org

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


Reply via email to