# Simple question

**URL:** <https://www.ruby-forum.com/t/simple-question/109908>\
**Category:** Ruby\
**Created:** [July 20, 2007, 2:46am UTC](https://www.ruby-forum.com/t/simple-question/109908 "2007-07-20T02:46:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [July 20, 2007, 2:46am UTC](https://www.ruby-forum.com/t/simple-question/109908/1 "2007-07-20T02:46:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [July 20, 2007, 4:22am UTC](https://www.ruby-forum.com/t/simple-question/109908/2 "2007-07-20T04:22:33Z")

</div>

anitawa wrote:

> 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](http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html)

cheers,  
mick

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [July 20, 2007, 4:24am UTC](https://www.ruby-forum.com/t/simple-question/109908/3 "2007-07-20T04:24:33Z")

</div>

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

On Jul 19, 2007, at 10:19 PM, Michael H. wrote:

> > 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](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

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [July 20, 2007, 4:25am UTC](https://www.ruby-forum.com/t/simple-question/109908/4 "2007-07-20T04:25:33Z")

</div>

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

anitawa wrote:

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

“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](http://enigmail.mozdev.org)

iD8DBQFGoBzPWvapaOIz2YYRAq0/AJ91/fhAjvzUTNPdorleu5KM39yFIACeI33D  
up+jMT1DWbZA/pjE63ihgZk=  
=EHig  
-----END PGP SIGNATURE-----
