[rcr] String#split behaves odd

Maybe the return value of String#split is wrong.
If I invoke split on an empty string, then it
results in an empty Array (which I think is odd).

    ''.split(/\n/) #=> []

I would have expected this to happen:

    ''.split(/\n/) #=> [""]

For instance JavaScript has a good split.

alert("".split(/\n/).length); // -> 1

Also Python has good split.

print "".split("\n"); # -> ['']

However Perl has a bad split.

$size = split(/\n/, "");
print "size is $size"; # -> 0

···

--
Simon Strandgaard

Perl's split() works as you described above. You just need to call it in "list context":

@arr = split /\n/, "";
# ...

James

···

On Dec 6, 2004, at 1:20 PM, Simon Strandgaard wrote:

However Perl has a bad split.

$size = split(/\n/, "");
print "size is $size"; # -> 0

Hi,

···

In message "Re: [rcr] String#split behaves odd" on Tue, 7 Dec 2004 04:20:37 +0900, Simon Strandgaard <neoneye@gmail.com> writes:

Maybe the return value of String#split is wrong.
If I invoke split on an empty string, then it
results in an empty Array (which I think is odd).

Feeling odd is subjective. Could you tell me why you felt
String#split is "wrong"?

              matz.

James Edward Gray II wrote:

> However Perl has a bad split.
>
> $size = split(/\n/, "");
> print "size is $size"; # -> 0

Perl's split() works as you described above. You just need to call

it

in "list context":

@arr = split /\n/, "";
# ...

Try

@arr = split /\n/, -1;

Which I think should be defualt behavior.

T.

···

On Dec 6, 2004, at 1:20 PM, Simon Strandgaard wrote: