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
|