[rcr] String#split behaves odd

  • Thread starter Simon Strandgaard
  • Start date
S

Simon Strandgaard

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
 
J

James Edward Gray II

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
 
T

tsawyer

James said:
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.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: [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).

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

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top