[lnkForumImage]
TotalShareware - Download Free Software

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


 

Joe Van Dyk

7/1/2005 9:49:00 PM

I thought for all of five seconds for a good subject line for this
question, but failed. Sorry!

I have a string like:

"some_key: blah, some_other_key: more_blah, yet_other_key: yet_more_blah"

I want to build up a hash like

{ :some_key => "blah", :some_other_key => "more_blah", :yet_other_key
=> "yet_more_blah" }

And I don't really want to have to know what the possible keys are in advance.

So, the message format looks like:
<key>: <value>, <key>: <value>

How can I properly extract it out?

Here's my initial attempt, which works, but seems hackish:

attributes = message.split(",")
attributes.each do |attribute|
key, value = attribute.scan(/(\w+): (.+)/)[0]
result_hash[key.to_sym] = value.strip
end

Also, this will get ran potentially thousands of times per second, so
executation speed is of some concern.


7 Answers

Tim Hunter

7/1/2005 10:58:00 PM

0

Joe Van Dyk wrote:
> I thought for all of five seconds for a good subject line for this
> question, but failed. Sorry!
>
> I have a string like:
>
> "some_key: blah, some_other_key: more_blah, yet_other_key: yet_more_blah"
>
> I want to build up a hash like
>
> { :some_key => "blah", :some_other_key => "more_blah", :yet_other_key
> => "yet_more_blah" }
>
> And I don't really want to have to know what the possible keys are in advance.
> So, the message format looks like:
> <key>: <value>, <key>: <value>
>
> How can I properly extract it out?
>
> Here's my initial attempt, which works, but seems hackish:
>
> attributes = message.split(",")
> attributes.each do |attribute|
> key, value = attribute.scan(/(\w+): (.+)/)[0]
> result_hash[key.to_sym] = value.strip
> end
>
> Also, this will get ran potentially thousands of times per second, so
> executation speed is of some concern.
>
>

It doesn't look particularly hackish to me, but maybe my sensibilities
aren't fine enough. The only thing I'd say is that if performance is
important then we ought to ask the regular expression to strip
whitespace around the key and value so we can avoid the #strip method.

Here's my version:

require 'pp'

hash = Hash.new
DATA.each do |line|
attrs = line.split(/,/)
attrs.each do |attr|
m = /\s*(\w+)\s*:\s*(\w+)\s*/.match(attr)
raise "#{attr.chomp} doesn't look like key:value" unless m
hash[m[1].intern] = m[2]
end
end

pp hash

__END__
some_key: blah, some_other_key: more_blah, yet_other_key: yet_more_blah
key1:value1
key2: value2
key3 : value 3 , key4 :value4 , key555555555:value55555



The output is:
{:some_other_key=>"more_blah",
:key555555555=>"value55555",
:yet_other_key=>"yet_more_blah",
:key1=>"value1",
:key2=>"value2",
:key3=>"value",
:some_key=>"blah",
:key4=>"value4"}

Devin Mullins

7/1/2005 11:23:00 PM

0

Joe Van Dyk wrote:

>Here's my initial attempt, which works, but seems hackish:
>
> attributes = message.split(",")
> attributes.each do |attribute|
> key, value = attribute.scan(/(\w+): (.+)/)[0]
> result_hash[key.to_sym] = value.strip
> end
>
>
Slightly more readable:

result_hash = {}
attributes = message.split(",")
attributes.each do |attribute|
key, value = *attribute.match(/(\w+): (.+)/).captures
result_hash[key.to_sym] = value.strip
end


Yet more readable:

result_hash = {}
attributes = message.split ","
attributes.each do |attribute|
key, value = *attribute.split(": ",2)
result_hash[key.to_sym] = value.strip
end

Not sure:
attributes = message.split ","
result_hash = attributes.inject {} do |hash,attribute|
key, value = *attribute.split(": ",2)
hash[key.to_sym] = value.strip
hash
end

>Also, this will get ran potentially thousands of times per second, so
>executation speed is of some concern.
>
>
No clue.

Devin



Ara.T.Howard

7/2/2005 12:42:00 AM

0

Daniel Brockman

7/2/2005 1:02:00 AM

0

Daniel Brockman

7/2/2005 2:10:00 AM

0

Ara.T.Howard

7/2/2005 3:06:00 AM

0

Daniel Brockman

7/2/2005 3:28:00 AM

0