Possible Duplicate:
Split problem in Ruby
In Ruby, when I split a string with a delimiter that matches the beginning of the string, it gives an empty string in the initial position of the array:
"abc".split(/a/) # => ["", "bc"]
but when I do a similar thing with a delimiter that matches the end of the string, it does not give an empty string:
"abc".split(/c/) # => ["ab"]
This looks inconsistent to me. Is there any rationale for such specification?
Edit
If it is to be compatible with Perl’s specification as in muu is to short’s answer, then the question remains the same: Why is it like that in Perl? And for this reason, now it also becomes a question about Perl.
After reading AWK’s specification following mu is too short, I came to feel that the original intention for
splitin AWK was to extract substrings that correspond to fields, each of which is terminated by a punctuation mark like,,., and the separator was considered something like an “end of field character”. The intention was not splitting a string symmetrically into the left and the right side of each separator position, but was terminating a substring on the left side of a separator position. Under this conception, it makes sense to always have some string (even if it is empty) on the left of the separator, but not necessarily on the right side of the separator. This may have been inherited to Ruby via Perl.