[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String parse

anitawa

8/14/2007 10:58:00 PM

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

4 Answers

James Gray

8/14/2007 11:16:00 PM

0

On Aug 14, 2007, at 6:00 PM, anitawa wrote:

> 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

Joel VanderWerf

8/15/2007 3:12:00 AM

0

James Edward Gray II wrote:
> On Aug 14, 2007, at 6:00 PM, anitawa wrote:
>
>> 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]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Olivier

8/15/2007 6:26:00 PM

0

Le mercredi 15 août 2007 01:00, anitawa a écrit :
> 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

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

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

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

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

--
Olivier Renaud

anitawa

8/15/2007 9:39:00 PM

0

On Aug 14, 8:12 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> James Edward Gray II wrote:
>
>
>
> > On Aug 14, 2007, at 6:00 PM, anitawa wrote:
>
> >> 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]
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks, just what i was looking for