[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

split with negative limit

T. Onoma

10/10/2004 4:19:00 AM

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.


4 Answers

Mark Hubbart

10/10/2004 4:55:00 AM

0

On Sun, 10 Oct 2004 13:18:48 +0900, trans. (T. Onoma)
<transami@runbox.com> wrote:
> 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.

>
> 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.
>
>


T. Onoma

10/10/2004 5:44:00 AM

0

On Sunday 10 October 2004 12:54 am, Mark Hubbart wrote:
| On Sun, 10 Oct 2004 13:18:48 +0900, trans. (T. Onoma)
|
| <transami@runbox.com> wrote:
| > 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.


Brian Candler

10/10/2004 10:42:00 AM

0

On Sun, Oct 10, 2004 at 01:18:48PM +0900, trans. (T. Onoma) wrote:
> 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.


ah

11/9/2008 12:28:00 AM

0

Eli Grubman wrote:
> On Wed, 29 Oct 2008 18:11:08 +0000, Naked Gonad
> <bodron57@tiscali.co.uk> wrote:
>
>>Eli Grubman wrote:
>>> On Wed, 29 Oct 2008 16:29:02 +0000, Naked Gonad
>>> <bodron57@tiscali.co.uk> wrote:
>>>
>>>> Eli Grubman wrote:
>>>>> On Wed, 29 Oct 2008 14:53:23 +0000, Naked Gonad
>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>
>>>>>> Eli Grubman wrote:
>>>>>>> On Wed, 29 Oct 2008 13:30:59 +0000, Naked Gonad
>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>
>>>>>>>> Eli Grubman wrote:
>>>>>>>>> On Wed, 29 Oct 2008 13:15:11 +0000, Naked Gonad
>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>
>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>> On Wed, 29 Oct 2008 11:43:02 +0000, Naked Gonad
>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>> On Wed, 29 Oct 2008 11:22:40 +0000, Naked Gonad
>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>> On Wed, 29 Oct 2008 09:26:26 +0000, Naked Gonad
>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>> On Wed, 29 Oct 2008 08:58:48 +0000, Naked Gonad
>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>> On Wed, 29 Oct 2008 08:12:01 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>> On Tue, 28 Oct 2008 22:22:03 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>> On Mon, 27 Oct 2008 21:01:53 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 25 Oct 2008 09:22:51 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>> On Thu, 23 Oct 2008 18:59:47 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 21 Oct 2008 20:53:17 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 21 Oct 2008 02:34:36 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Mon, 20 Oct 2008 18:54:34 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 19 Oct 2008 17:30:45 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 19 Oct 2008 09:19:44 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 18 Oct 2008 22:57:27 -0400, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Peter Hucker wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 11 Oct 2008 00:52:11 +0100, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Peter Hucker wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 10 Oct 2008 02:30:35 +0100, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Peter Hucker wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Mon, 06 Oct 2008 02:17:03 +0100, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Peter Hucker wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 05 Oct 2008 02:18:30 +0100, ah <splifingate@gmail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> rand mair fheal wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I'm getting separations anxiety!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Actually he's wrong. You used RGB.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yes, I did. Hence the assertion that I was anxious about separating my
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> colour-space!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> There is no such colourspace as CMY,
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Aw con't rare.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> it would be CMYK, and you didn't specify four values.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> From <http://en.wikipedia.org/wiki/CMYK_color...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "Why black ink is used
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The ?black? generated by mixing Cyan, Magenta and Yellow primaries is
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> unsatisfactory, and so four-color printing uses black ink in addition to the
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> subtractive primaries. Common reasons for using black ink include:[5]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> * Text is typically printed in black and includes fine detail (such as
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> serifs), so to reproduce text or other finely detailed outlines using three
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> inks without slight blurring would require impractically accurate registration
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (i.e. all three images would need to be aligned extremely precisely).
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> * A combination of 100% cyan, magenta, and yellow inks soaks the paper
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> with ink, making it slower to dry, and sometimes impractically so.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> * A combination of 100% cyan, magenta, and yellow inks often results in a
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> muddy dark brown color that does not quite appear black. Adding black ink
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> absorbs more light, and yields much darker blacks.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> * Using black ink is less expensive than using the corresponding amounts
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> of colored inks.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> When a very dark area is desirable, a colored or gray CMY ?bedding? is applied
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> first, then a full black layer is applied on top, making a rich, deep black;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> this is called rich black.[6] A black made with just CMY inks is sometimes
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> called a composite black.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The amount of black to use to replace amounts of the other ink is variable,
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> and the choice depends on the technology, paper and ink in use. Processes
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> called under color removal, under color addition, and gray component
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> replacement are used to decide on the final mix; different CMYK recipes will
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> be used depending on the printing task."
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> If I don't need black, it isn't used.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> My photo editor refers to RGB, CMYK, and HSL.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> http://www.couleur.org/index.php?page=trans...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Arctan?!?! Fucking hell. What is the point of all these? RGB for light (monitors), and CMYK for ink (printers). What's the need for anything else?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You said--and I quote--"There is no such colourspace as CMY."
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Q.E.D.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Ok, no such useful colourspace. You try mixing C M and Y. You will not get black. More likely the colour of diarhea.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Non-sequitur, Peter.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On your part, a"h.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Still suffering from attribution problems, Grubman?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Non sequitur, laced with invective, as can be expected from someone
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> who lacks a logical answer, a"h.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Who is a"h, Grubman? Still suffering from attribution problems?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Classic evasion, a"h.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Who is a"h, Grubman? Still suffering from attribution problems?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Note: no response.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Non sequitur.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On your part, a"h.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Note: no response.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Classic evasion, a"h.
>>>>>>>>>>>>>>>>>>>>>>>>>>>> Who is a"h, Grubman? Still suffering from attribution problems?
>>>>>>>>>>>>>>>>>>>>>>>>>>> Note: non sequitur laced with invective.
>>>>>>>>>>>>>>>>>>>>>>>>>> Classic erroneous and unsubstantiated claim, Grubman.
>>>>>>>>>>>>>>>>>>>>>>>>> Classic evasion laced with invective, as can be expected from someone
>>>>>>>>>>>>>>>>>>>>>>>>> who lacks a logical argument, a"h.
>>>>>>>>>>>>>>>>>>>>>>>> Who is a"h, Grubman? Still suffering from attribution problems?
>>>>>>>>>>>>>>>>>>>>>>> Note: no response.
>>>>>>>>>>>>>>>>>>>>>> How ironic.
>>>>>>>>>>>>>>>>>>>>> Classic evasion.
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> Agreed.
>>>>>>>>>>>>>>>>>>> Classic concurrence.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Repetitive evasive responses.
>>>>>>>>>>>>>>>>> Classic inconsistence.
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Monotonous irrelevance.
>>>>>>>>>>>>>>> Deliberate misinterpretation.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Blaze retorts.
>>>>>>>>>>>>> Intentionally misconstrued.
>>>>>>>>>>>>>
>>>>>>>>>>>> Verbal dihrea.
>>>>>>>>>>> Cranial constipation.
>>>>>>>>>>>
>>>>>>>>>> Mouth malfunction.
>>>>>>>>> Digital transposition.
>>>>>>>>>
>>>>>>>> Text abuse.
>>>>>>> Literal degeneracy.
>>>>>>>
>>>>>> Rampant rhetoric
>>>>> Poetic incontinence.
>>>>>
>>>> Inappropriate reply.
>>>
>>> Unsuitable response.
>>>
>>Bogus answer
>
> Bollock bleating.
>

On your part, Grubman.
--
ah