Chas Owens wrote:
On 5/11/07, oryann9 <[EMAIL PROTECTED]> wrote:
> > > Now I am trying to break up string into
> individual
> > > chars, but this does not seem to work:
> > snip
> >
> > The idiomatic way is
> >
> > for my $chr (split //, $str) {
> > }
>
> Funny I had to explain split /|/, $str returning an
> array of characters.
>
> --
> Ken Foskey
> FOSS developer
>
Excellent Ken,
thank you, but why the pipe | and how does this differ
from ' ' or \s+. I used Dumper and it showed the
array of chars, but want to understand.
Any pattern that can match nothing (a so-called null pattern) will
split a string on nothing leaving you with a list of characters. ' '
and \s+ matches all consecutive white space, so they are not null
patterns and split on the matched value (whitespace).
#!/usr/bin/perl
use strict;
use warnings;
my $str = "abc def ghi";
print join(",", split //, $str),"\n";
print join(",", split /(?:ABC)*/, $str),"\n";
print join(",", split /|/, $str),"\n";
print join(",", split " ", $str),"\n";
print join(",", split /\s+/, $str),"\n";
a,b,c, ,d,e,f, ,g,h,i
a,b,c, ,d,e,f, ,g,h,i
a,b,c, ,d,e,f, ,g,h,i
abc,def,ghi
abc,def,ghi
use strict;
use warnings;
my $str = ' abc def ghi ';
print join(",", split " ", $str),"\n";
print join(",", split /\s+/, $str),"\n";
**OUTPUT**
abc,def,ghi
,abc,def,ghi
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/