split question

Discussion in 'Ruby' started by Li Chen, Oct 7, 2009.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I try to split a line that is separated by 'tab' and expect return an
    array with all the elements containing empty space. But my code doesn't
    work. Any idea?

    Thanks,

    Li

    C:\Documents and Settings\chen41\Desktop>irb
    irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    => "\\t\\t\\t\\t\\t\\t\\t\\t"
    irb(main):002:0> s.split(/\t/)
    => ["\\t\\t\\t\\t\\t\\t\\t\\t"]
    irb(main):003:0> s.split(/\\t/)
    => []
    irb(main):004:0>
    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 7, 2009
    #1
    1. Advertising

  2. Li Chen

    Ryan Davis Guest

    On Oct 7, 2009, at 14:29 , Li Chen wrote:

    > Hi all,
    >
    > I try to split a line that is separated by 'tab' and expect return an
    > array with all the elements containing empty space. But my code
    > doesn't
    > work. Any idea?
    >
    > Thanks,
    >
    > Li
    >
    > C:\Documents and Settings\chen41\Desktop>irb
    > irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    > => "\\t\\t\\t\\t\\t\\t\\t\\t"


    These aren't tabs. They're "\" followed by "t", you can tell because
    the "\" is backslashed, meaning it is a literal backslash.

    Use double quotes instead.

    > irb(main):002:0> s.split(/\t/)
    > => ["\\t\\t\\t\\t\\t\\t\\t\\t"]
    > irb(main):003:0> s.split(/\\t/)
    > => []
    > irb(main):004:0>
    > --
    > Posted via http://www.ruby-forum.com/.
    >
     
    Ryan Davis, Oct 7, 2009
    #2
    1. Advertising

  3. Li Chen

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Wed, Oct 7, 2009 at 4:29 PM, Li Chen <> wrote:

    > Hi all,
    >
    > I try to split a line that is separated by 'tab' and expect return an
    > array with all the elements containing empty space. But my code doesn't
    > work. Any idea?
    >
    > Thanks,
    >
    > Li
    >
    > C:\Documents and Settings\chen41\Desktop>irb
    > irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    > => "\\t\\t\\t\\t\\t\\t\\t\\t"
    > irb(main):002:0> s.split(/\t/)
    > => ["\\t\\t\\t\\t\\t\\t\\t\\t"]
    > irb(main):003:0> s.split(/\\t/)
    > => []
    > irb(main):004:0>
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Single quotes treat things literally, like you can't interpolate, and \t is
    treated as two separate characters.

    $irb

    >> RUBY_VERSION

    => "1.8.6"

    >> s = '\t\t\t\t\t\t\t\t'

    => "\\t\\t\\t\\t\\t\\t\\t\\t"

    >> s = "\t\t\t\t\t\t\t\t"

    => "\t\t\t\t\t\t\t\t"

    >> s.split /\t/

    => []

    >> 'abc#{2}def'

    => "abc\#{2}def"

    >> "abc#{2}def"

    => "abc2def"
     
    Josh Cheek, Oct 7, 2009
    #3
  4. Li Chen

    Li Chen Guest

    Ryan Davis wrote:
    > On Oct 7, 2009, at 14:29 , Li Chen wrote:
    >
    >>
    >> C:\Documents and Settings\chen41\Desktop>irb
    >> irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    >> => "\\t\\t\\t\\t\\t\\t\\t\\t"

    >
    > These aren't tabs. They're "\" followed by "t", you can tell because
    > the "\" is backslashed, meaning it is a literal backslash.
    >
    > Use double quotes instead.


    Here is what I get using double quotes. I get an empty array only.


    C:\Documents and Settings\chen41\Desktop>irb
    irb(main):001:0> s="\t\t\t\t\t\t\t\t"
    => "\t\t\t\t\t\t\t\t"
    irb(main):002:0> s.split(/\t/)
    => []
    irb(main):003:0>

    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 7, 2009
    #4
  5. Li Chen wrote:
    > Ryan Davis wrote:
    >> On Oct 7, 2009, at 14:29 , Li Chen wrote:
    >>
    >>>
    >>> C:\Documents and Settings\chen41\Desktop>irb
    >>> irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    >>> => "\\t\\t\\t\\t\\t\\t\\t\\t"

    >>
    >> These aren't tabs. They're "\" followed by "t", you can tell because
    >> the "\" is backslashed, meaning it is a literal backslash.
    >>
    >> Use double quotes instead.

    >
    > Here is what I get using double quotes. I get an empty array only.
    >
    >
    > C:\Documents and Settings\chen41\Desktop>irb
    > irb(main):001:0> s="\t\t\t\t\t\t\t\t"
    > => "\t\t\t\t\t\t\t\t"
    > irb(main):002:0> s.split(/\t/)
    > => []
    > irb(main):003:0>


    str.split takes two parameters: the pattern and optionally the "limit"
    From the docs:
    If the limit parameter is omitted, trailing null fields are suppressed.
    If limit is a positive number, at most that number of fields will be
    returned (if limit is 1, the entire string is returned as the only entry
    in an array). If negative, there is no limit to the number of fields
    returned, and trailing null fields are not suppressed.

    So

    s = "\t\t\t\t"
    s.split("\t") #all null fields
    #=> []
    s.split("\t",-1)
    #=> ["", "", "", ""]

    hth,

    Siep

    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Oct 7, 2009
    #5
  6. Hi --

    On Thu, 8 Oct 2009, Siep Korteling wrote:

    > Li Chen wrote:
    >> Ryan Davis wrote:
    >>> On Oct 7, 2009, at 14:29 , Li Chen wrote:
    >>>
    >>>>
    >>>> C:\Documents and Settings\chen41\Desktop>irb
    >>>> irb(main):001:0> s='\t\t\t\t\t\t\t\t'
    >>>> => "\\t\\t\\t\\t\\t\\t\\t\\t"
    >>>
    >>> These aren't tabs. They're "\" followed by "t", you can tell because
    >>> the "\" is backslashed, meaning it is a literal backslash.
    >>>
    >>> Use double quotes instead.

    >>
    >> Here is what I get using double quotes. I get an empty array only.
    >>
    >>
    >> C:\Documents and Settings\chen41\Desktop>irb
    >> irb(main):001:0> s="\t\t\t\t\t\t\t\t"
    >> => "\t\t\t\t\t\t\t\t"
    >> irb(main):002:0> s.split(/\t/)
    >> => []
    >> irb(main):003:0>

    >
    > str.split takes two parameters: the pattern and optionally the "limit"
    >> From the docs:

    > If the limit parameter is omitted, trailing null fields are suppressed.
    > If limit is a positive number, at most that number of fields will be
    > returned (if limit is 1, the entire string is returned as the only entry
    > in an array). If negative, there is no limit to the number of fields
    > returned, and trailing null fields are not suppressed.
    >
    > So
    >
    > s = "\t\t\t\t"
    > s.split("\t") #all null fields
    > #=> []
    > s.split("\t",-1)
    > #=> ["", "", "", ""]


    ["", "", "", "", ""] I think :)


    David

    --
    The Ruby training with D. Black, G. Brown, J.McAnally
    Compleat Jan 22-23, 2010, Tampa, FL
    Rubyist http://www.thecompleatrubyist.com

    David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Oct 7, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    490
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    735
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    236
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    274
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    702
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page