Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String parse

Reply
Thread Tools

String parse

 
 
anitawa
Guest
Posts: n/a
 
      08-14-2007
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

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-14-2007
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

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-15-2007
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

 
Reply With Quote
 
Olivier Renaud
Guest
Posts: n/a
 
      08-15-2007
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

 
Reply With Quote
 
anitawa
Guest
Posts: n/a
 
      08-15-2007
On Aug 14, 8:12 pm, Joel VanderWerf <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Is there a buit-in method to parse a STYLE attribute string? Henri ASP .Net 2 11-29-2004 02:59 AM
Perl - Parse UNC Path in a string variable Kevin Joseph Perl 5 08-13-2004 11:56 AM
parse string Vincent Perl 2 12-16-2003 08:11 PM



Advertisments