split with negative limit

T

trans. (T. Onoma)

Word o' warning. I just put this comment in a program:

lines = self.split("\n",-1) # boy oh boy was that -1 a pain to figure out!

All told I probably wasted half of my day trying to figure this out because it
was causing another part of the program to act oddly (which I still don't
quite get but nonetheless), thus digusing the real issue.

This is VERY unintuitive. The value itself doesn't even /do/ anything when the
limit is negative. Moreover, is the default of supressing null fields really
best? Recommend improvement something like:

def split( pattern=$; , supres_null=false , limit=nil )

No negative number trick.

Thanks,
T.
 
M

Mark Hubbart

Word o' warning. I just put this comment in a program:

lines = self.split("\n",-1) # boy oh boy was that -1 a pain to figure out!

???

str = "one\n\ntwo\n\nthree"
==>"one\n\ntwo\n\nthree"
str.split("\n")
==>["one", "", "two", "", "three"]
str.split("\n",-1)
==>["one", "", "two", "", "three"]
RUBY_VERSION
==>"1.9.0"

What did you gain by adding the -1 in? I tried this same thing using
ruby 1.6.8, got the same results.

Null field filtering is normally only done when no arguments are
passed, or with the special case " " argument.
 
T

trans. (T. Onoma)

18:48 +0900, trans. (T. Onoma)
|
| > Word o' warning. I just put this comment in a program:
| >
| > lines = self.split("\n",-1) # boy oh boy was that -1 a pain to figure
| > out!
|
| ???
|
| str = "one\n\ntwo\n\nthree"
| ==>"one\n\ntwo\n\nthree"
| str.split("\n")
| ==>["one", "", "two", "", "three"]
| str.split("\n",-1)
| ==>["one", "", "two", "", "three"]
| RUBY_VERSION
| ==>"1.9.0"
|
| What did you gain by adding the -1 in? I tried this same thing using
| ruby 1.6.8, got the same results.

irb(main):001:0> str = "one\n\ntwo\n\nthree\n\n"
=> "one\n\ntwo\n\nthree\n\n"
irb(main):002:0> str.split("\n")
=> ["one", "", "two", "", "three"]
irb(main):003:0> str.split("\n",-1)
=> ["one", "", "two", "", "three", "", ""]

| Null field filtering is normally only done when no arguments are
| passed, or with the special case " " argument.

Without the -1 I was loosing all my remaining blank lines.

(using 1.8.2)

T.
 
B

Brian Candler

Word o' warning. I just put this comment in a program:

lines = self.split("\n",-1) # boy oh boy was that -1 a pain to figure out!

All told I probably wasted half of my day trying to figure this out because it
was causing another part of the program to act oddly (which I still don't
quite get but nonetheless), thus digusing the real issue.

This is VERY unintuitive.

It's taken from Perl. See 'man perlfunc':

split /PATTERN/,EXPR,LIMIT
...
If LIMIT is specified and positive, splits into no more than
that many fields (though it may split into fewer). If LIMIT is
unspecified or zero, trailing null fields are stripped (which
potential users of "pop" would do well to remember). If LIMIT
is negative, it is treated as if an arbitrarily large LIMIT had
been specified.

Perhaps in the early days, Ruby being similar to Perl was considered a good
thing :)

Regards,

Brian.
 

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

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top