String parse

A

anitawa

Hello,

I want to be able to parse a string and put them into variables.

for example, I have this string:

"Menu: steak_and_egg | date: 0814 | who: Anita"

I want to parse this string to assign variables like so:

menu = "steak_and_egg"
date = "0814"
who = "Anita"

What would be the fastest way of doing this.

Thanks
 
J

James Edward Gray II

Hello,

I want to be able to parse a string and put them into variables.

for example, I have this string:

"Menu: steak_and_egg | date: 0814 | who: Anita"

I want to parse this string to assign variables like so:

menu = "steak_and_egg"
date = "0814"
who = "Anita"

What would be the fastest way of doing this.

This might work if your data isn't too complicated:
str = "Menu: steak_and_egg | date: 0814 | who: Anita" => "Menu: steak_and_egg | date: 0814 | who: Anita"
Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]
=> {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}

James Edward Gray II
 
J

Joel VanderWerf

James said:
Hello,

I want to be able to parse a string and put them into variables.

for example, I have this string:

"Menu: steak_and_egg | date: 0814 | who: Anita"

I want to parse this string to assign variables like so:

menu = "steak_and_egg"
date = "0814"
who = "Anita"

What would be the fastest way of doing this.

This might work if your data isn't too complicated:
str = "Menu: steak_and_egg | date: 0814 | who: Anita" => "Menu: steak_and_egg | date: 0814 | who: Anita"
Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]
=> {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}

Might be more general than is needed. If you know in advance that the
"fields" are menu, date, and who, then this will do:

str = "Menu: steak_and_egg | date: 0814 | who: Anita"
pat = /Menu: (\S+) \| date: (\S+) \| who: (\S+)/

menu, date, who = str.scan(pat)[0]
 
O

Olivier Renaud

Le mercredi 15 ao=FBt 2007 01:00, anitawa a =E9crit=A0:
Hello,

I want to be able to parse a string and put them into variables.

for example, I have this string:

"Menu: steak_and_egg | date: 0814 | who: Anita"

I want to parse this string to assign variables like so:

menu =3D "steak_and_egg"
date =3D "0814"
who =3D "Anita"

What would be the fastest way of doing this.

Thanks

This is a CSV format, You can use the csv lib from the stdlib :

# parse with : and | as delimiters
str =3D "Menu: steak_and_egg | date: 0814 | who: Anita"
parsed =3D CSV.parse(str, ":", "|")
=3D> [["Menu", " steak_and_egg "], [" date", " 0814 "], [" who", " Anita"]]

# then, put the results in a hash
res =3D {}
parsed.each do |k, v|
res[k.strip] =3D v.strip
end
=3D> {"date"=3D>"0814", "who"=3D>"Anita", "Menu"=3D>"steak_and_egg"}

You can also use Enumerable#inject for putting in the Hash (or Hash::[] wit=
h=20
some adaptations)

=2D-=20
Olivier Renaud
 
A

anitawa

James Edward Gray II wrote:


On Aug 14, 2007, at 6:00 PM, anitawa wrote:
This might work if your data isn't too complicated:
str = "Menu: steak_and_egg | date: 0814 | who: Anita"
=> "Menu: steak_and_egg | date: 0814 | who: Anita"
Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]
=> {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}

Might be more general than is needed. If you know in advance that the
"fields" are menu, date, and who, then this will do:

str = "Menu: steak_and_egg | date: 0814 | who: Anita"
pat = /Menu: (\S+) \| date: (\S+) \| who: (\S+)/

menu, date, who = str.scan(pat)[0]

Thanks, just what i was looking for
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top