simple question

Discussion in 'Ruby' started by anitawa, Jul 20, 2007.

  1. anitawa

    anitawa Guest

    hello

    I have a string like this

    string = "hello -a apple -b butterfly -c cat -d dophin"

    i would like to parse the string based on some reg exp so that i can
    store it in a variable like so.

    valuea = "apple"
    valueb = "butterfly"
    valuec = "cat"
    valued = "dophin"

    Could someone help me out.
    anitawa, Jul 20, 2007
    #1
    1. Advertising

  2. anitawa

    Ari Brown Guest

    or a gem called 'OptionParser'. It's like optparse, only, I find, a
    lot simpler.

    On Jul 19, 2007, at 10:19 PM, Michael Hollins wrote:

    > anitawa wrote:
    >> hello
    >> I have a string like this
    >> string = "hello -a apple -b butterfly -c cat -d dophin"
    >> i would like to parse the string based on some reg exp so that i can
    >> store it in a variable like so.
    >> valuea = "apple"
    >> valueb = "butterfly"
    >> valuec = "cat"
    >> valued = "dophin"
    >> Could someone help me out.

    >
    > Are you parsing a command line? If so, you might want to consider
    > http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
    >
    > cheers,
    > mick
    >
    >


    ---------------------------------------------------------------|
    ~Ari
    "I don't suffer from insanity. I enjoy every minute of it" --1337est
    man alive
    Ari Brown, Jul 20, 2007
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    anitawa wrote:
    > hello
    >
    > I have a string like this
    >
    > string = "hello -a apple -b butterfly -c cat -d dophin"
    >
    > i would like to parse the string based on some reg exp so that i can
    > store it in a variable like so.
    >
    > valuea = "apple"
    > valueb = "butterfly"
    > valuec = "cat"
    > valued = "dophin"


    Here you go, but I just had to make one change. Instead of having variables
    valuea, valueb, valuec..., I put them in a hash called value. Is it absolutely
    necessary to have them as you posted?

    string.split(/\s-/).each do |v|
    v = v.split(/\s/)
    value[ v[0] ] = v[1] if v.size == 2
    end

    So you would have

    value["a"] = "apple"
    value["b"] = "butterfly"
    etc...

    - --
    Travis Warlick

    "Programming in Java is like dealing with your mom --
    it's kind, forgiving, and gently chastising.
    Programming in C++ is like dealing with a disgruntled
    girlfriend -- it's cold, unforgiving, and doesn't tell
    you what you've done wrong."
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFGoBzPWvapaOIz2YYRAq0/AJ91/fhAjvzUTNPdorleu5KM39yFIACeI33D
    up+jMT1DWbZA/pjE63ihgZk=
    =EHig
    -----END PGP SIGNATURE-----
    Travis D Warlick Jr, Jul 20, 2007
    #3
    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:
    0
    Views:
    543
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    646
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    822
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    360
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    201
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page