simple question

A

anitawa

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

Travis D Warlick Jr

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top