Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl split

Reply
Thread Tools

perl split

 
 
James
Guest
Posts: n/a
 
      08-25-2010
This I understand,
$ perl -le '@a=split//,"abc"; print $#a; print for(@a)'
2
a
b
c

But why this behavior?
$ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
5

a

b

c

TIA
-James
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      08-25-2010
On 2010-08-25 19:56, James wrote:

> This I understand,
> $ perl -le '@a = split //, "abc"; print $#a; print for @a'
> 2 [...]
>
> But why this behavior?
> $ perl -le '@a = split /(.)/, "abc"; print $#a; print for @a'
> 5 [...]


See perldoc -f split, look for 'parentheses'.

--
Ruud
 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      08-25-2010
On 25 août, 19:56, James <hslee...@yahoo.com> wrote:
> But why this behavior?
> $ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
> 5
>
> a
>
> b
>
> c


Looks normal to me: you split with any character as separator, so you
get:

- an empty string before the separator 'a'
- then, because you have put brackets into the regex, the separator
('a' in this case) is added
- then another empty string between the two separators 'a' and 'b'
- then, again, you have put brackets into the regex, so you get the
second separator ('b' in this case)
- then yet another empty string between the two separators 'b' and 'c'
- finally you get the last separator 'c'
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-26-2010
Klaus wrote:
> On 25 août, 19:56, James<hslee...@yahoo.com> wrote:
>> But why this behavior?
>> $ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
>> 5
>>
>> a
>>
>> b
>>
>> c

>
> Looks normal to me: you split with any character as separator, so you
> get:
>
> - an empty string before the separator 'a'
> - then, because you have put brackets into the regex, the separator
> ('a' in this case) is added
> - then another empty string between the two separators 'a' and 'b'
> - then, again, you have put brackets into the regex, so you get the
> second separator ('b' in this case)
> - then yet another empty string between the two separators 'b' and 'c'
> - finally you get the last separator 'c'


And finally you get the last empty string between 'c' and the
end-of-string, which is discarded because it is a trailing empty string.
If you want to see it use a negative number as the third argument to
split:

$ perl -le '@a=split/(.)/,"abc",-1; print $#a; print for(@a)'
6

a

b

c




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57