Why is slice ignoring my fields at the end of the string?

I’m not sure if this is a bug with the slice method, or if I’m just
misunderstanding the logic behind it. If I have a string “1,2,3” that I
slice into an array, I get 3 values.

But if I have “1,2,3,” with a trailing comma, I still get 3 – even
though I probably should have gotten 4, with 4 being an empty string.

If I have “1,2,3” then I will get 4 elements with the third one
properly being an empty string.

The trailing delimiter problem seems to repeat – if I have “1,2,3,”
then I still end up with an array of just 3 elements. All of ones on
the end are gone.

Anybody know if this is the intended behavior, and if so, why? I’d
think that if I’m saying delimit fields on this character, then it would
be up to me to decide whether or not to keep the empties. Maybe there’s
a way to call split with a flag that says to keep the empties, I’m not
sure. That just occurred to me, gonna go look into that.

D

I wrote that entire post saying “slice” when what I meant to say was
“split”.
Splitting a string into an array using a specified delimiter.

Sorry about that!

D
Duane M. wrote:

I’m not sure if this is a bug with the slice method, or if I’m just
misunderstanding the logic behind it. If I have a string “1,2,3” that I
slice into an array, I get 3 values.

But if I have “1,2,3,” with a trailing comma, I still get 3 – even
though I probably should have gotten 4, with 4 being an empty string.

If I have “1,2,3” then I will get 4 elements with the third one
properly being an empty string.

The trailing delimiter problem seems to repeat – if I have “1,2,3,”
then I still end up with an array of just 3 elements. All of ones on
the end are gone.

Anybody know if this is the intended behavior, and if so, why? I’d
think that if I’m saying delimit fields on this character, then it would
be up to me to decide whether or not to keep the empties. Maybe there’s
a way to call split with a flag that says to keep the empties, I’m not
sure. That just occurred to me, gonna go look into that.

D

Found it – there’s a “limit” parameter that can be passed to split. If
the value is non-zero, that affects whether null fields are returned as
part of the result. I don’t fully understand why that is (specifying -1
says to give me all of my fields regardless of how many I have,
including nulls), so I wasn’t exactly looking for such a thing.

d
Duane M. wrote:

I wrote that entire post saying “slice” when what I meant to say was
“split”.
Splitting a string into an array using a specified delimiter.

Sorry about that!

D
Duane M. wrote:

I’m not sure if this is a bug with the slice method, or if I’m just
misunderstanding the logic behind it. If I have a string “1,2,3” that I
slice into an array, I get 3 values.

But if I have “1,2,3,” with a trailing comma, I still get 3 – even
though I probably should have gotten 4, with 4 being an empty string.

If I have “1,2,3” then I will get 4 elements with the third one
properly being an empty string.

The trailing delimiter problem seems to repeat – if I have “1,2,3,”
then I still end up with an array of just 3 elements. All of ones on
the end are gone.

Anybody know if this is the intended behavior, and if so, why? I’d
think that if I’m saying delimit fields on this character, then it would
be up to me to decide whether or not to keep the empties. Maybe there’s
a way to call split with a flag that says to keep the empties, I’m not
sure. That just occurred to me, gonna go look into that.

D

On Tue, 21 Nov 2006, Duane M. wrote:

I’m not sure if this is a bug with the slice method, or if I’m just
misunderstanding the logic behind it. If I have a string “1,2,3” that I
slice into an array, I get 3 values.

OK, but using what code?
----------------------------------------------------------- String#slice
[…]
str.slice(fixnum) => fixnum or nil
str.slice(fixnum, fixnum) => new_str or nil
str.slice(range) => new_str or nil
str.slice(regexp) => new_str or nil
str.slice(regexp, fixnum) => new_str or nil
str.slice(other_str) => new_str or nil

[…]

 If a +Regexp+ is supplied, the matching portion of _str_ is
 returned. If a numeric parameter follows the regular expression,
 that component of the +MatchData+ is returned instead. If a
 +String+ is given, that string is returned if it occurs in _str_.
 In both cases, +nil+ is returned if there is no match.

I think you must be using /\d+/ or something.

Maybe what you want is split(/,/) ?

    [...]

Anybody know if this is the intended behavior, and if so, why? I’d

You will need to show us the code.

D

    Hugh