simple gsub question \' \` what?

Discussion in 'Ruby' started by Dustin Anderson, Mar 30, 2007.

  1. Sorry for the simple question, I just can't figure this out after a
    couple hours...

    I need to parse a string:

    string = column-1:block-0,block-2,block-1,block-3

    and I need to extract everything before the : (colon) into one variable
    (should end up like this:
    string1 = column-1

    and then the rest of the string (block-0,block-2,block-1,block3) into an
    array...

    How can I use gsub to extract everything before the colon?

    I've looked at the documentation and just can't understand how to use
    the \` or \' syntax... I want the two new strings to be:

    string1 = column-1
    string2 = ["block-0", "block-2", "block-1", "block-3"]

    I'm thinking it's something like this:

    string.gsub(/:/, "\`")

    really, I just don't get it...

    the documentation that I'm looking at is here:
    http://dev.rubycentral.com/ref/ref_c_string.html

    thanks a bunch!
    -Dustin

    --
    Posted via http://www.ruby-forum.com/.
     
    Dustin Anderson, Mar 30, 2007
    #1
    1. Advertising

  2. Alle venerd=C3=AC 30 marzo 2007, Dustin Anderson ha scritto:
    > Sorry for the simple question, I just can't figure this out after a
    > couple hours...
    >=20
    > I need to parse a string:
    >=20
    > string =3D column-1:block-0,block-2,block-1,block-3
    >=20
    > and I need to extract everything before the : (colon) into one variable
    > (should end up like this:
    > string1 =3D column-1
    >=20
    > and then the rest of the string (block-0,block-2,block-1,block3) into an
    > array...
    >=20
    > How can I use gsub to extract everything before the colon?
    >=20
    > I've looked at the documentation and just can't understand how to use
    > the \` or \' syntax... I want the two new strings to be:
    >=20
    > string1 =3D column-1
    > string2 =3D ["block-0", "block-2", "block-1", "block-3"]
    >=20
    > I'm thinking it's something like this:
    >=20
    > string.gsub(/:/, "\`")
    >=20
    > really, I just don't get it...
    >=20
    > the documentation that I'm looking at is here:
    > http://dev.rubycentral.com/ref/ref_c_string.html
    >=20
    > thanks a bunch!
    > -Dustin
    >=20


    I don't think you need gsub here. gsub is used to replace parts of a string=
    =20
    with other characters. What you need is a regexp. This should work:

    string.match( /^([^:]*):(.*)$/ )
    string1=3D$1
    string2=3D$2.split(',')

    The regexp will put anything from the beginning of the string until the=20
    first : (excluded) in the global variable $1 and anything after the : in th=
    e=20
    variable $2. You can then convert $2 to an array of string using the=20
    String#split method.

    I hope this helps

    Stefano
     
    Stefano Crocco, Mar 30, 2007
    #2
    1. Advertising

  3. Dustin Anderson wrote:
    > Sorry for the simple question, I just can't figure this out after a
    > couple hours...
    >
    > I need to parse a string:
    >
    > string = column-1:block-0,block-2,block-1,block-3
    >
    > and I need to extract everything before the : (colon) into one variable
    > (should end up like this:
    > string1 = column-1
    >
    > and then the rest of the string (block-0,block-2,block-1,block3) into an
    > array...
    >

    I'd use two calls to split:

    irb(main):001:0> string = 'column-1:block-0,block-2,block-1,block-3'
    => "column-1:block-0,block-2,block-1,block-3"
    irb(main):002:0> string1, s = string.split(':')
    => ["column-1", "block-0,block-2,block-1,block-3"]
    irb(main):003:0> string1
    => "column-1"
    irb(main):004:0> string2 = s.split(',')
    => ["block-0", "block-2", "block-1", "block-3"]
    irb(main):005:0>

    You could probably turn that into a one-liner but that's just golfing.
     
    Timothy Hunter, Mar 30, 2007
    #3
  4. On Mar 30, 2007, at 5:39 PM, Timothy Hunter wrote:

    > Dustin Anderson wrote:
    >> Sorry for the simple question, I just can't figure this out after a
    >> couple hours...
    >>
    >> I need to parse a string:
    >>
    >> string = column-1:block-0,block-2,block-1,block-3
    >>
    >> and I need to extract everything before the : (colon) into one
    >> variable
    >> (should end up like this:
    >> string1 = column-1
    >>
    >> and then the rest of the string (block-0,block-2,block-1,block3)
    >> into an
    >> array...
    >>

    > I'd use two calls to split:
    >
    > irb(main):001:0> string = 'column-1:block-0,block-2,block-1,block-3'
    > => "column-1:block-0,block-2,block-1,block-3"
    > irb(main):002:0> string1, s = string.split(':')
    > => ["column-1", "block-0,block-2,block-1,block-3"]
    > irb(main):003:0> string1
    > => "column-1"
    > irb(main):004:0> string2 = s.split(',')
    > => ["block-0", "block-2", "block-1", "block-3"]
    > irb(main):005:0>
    >
    > You could probably turn that into a one-liner but that's just golfing.
    >


    Here's a bit of "live" code that does the same thing (plus .to_f on
    the elements before turning them into a Vector, but you can knock
    that part out yourself)

    def add line
    id, vals = line.split(/:\s*/, 2)
    @labels << id
    @data << Vector[*(vals.split(',').map {|v| v.to_f})]
    end

    Note that the second arg (2) to split is important if a ':' can occur
    anywhere in your block-N parts. Of course, you just need vals.split
    (',')

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Mar 30, 2007
    #4
    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. Daniel Bretoi

    gsub question

    Daniel Bretoi, Dec 17, 2003, in forum: Ruby
    Replies:
    2
    Views:
    122
    Jamis Buck
    Dec 17, 2003
  2. Dirk Einecke

    Syntax question for gsub

    Dirk Einecke, Jul 11, 2004, in forum: Ruby
    Replies:
    11
    Views:
    444
    Chris Dutton
    Jul 12, 2004
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    184
    Robert Klemme
    Nov 9, 2005
  4. x1
    Replies:
    4
    Views:
    154
  5. Jack Smith

    simple gsub method

    Jack Smith, Oct 1, 2008, in forum: Ruby
    Replies:
    4
    Views:
    228
    Jack Smith
    Oct 1, 2008
Loading...

Share This Page