[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[rcr] String#split behaves odd

Simon Strandgaard

12/6/2004 7:21:00 PM

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


3 Answers

James Gray

12/6/2004 7:27:00 PM

0

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

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

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

James



tsawyer

12/6/2004 8:25:00 PM

0


James Edward Gray II wrote:
> 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
>
> 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.

Yukihiro Matsumoto

12/7/2004 12:14:00 AM

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.