[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash from string

Mage

8/6/2008 4:03:00 PM

Hello,

I have a string like: ' "gaz"=>"1", "viz"=>"1", "lift"=>"0",
"kamra"=>"1", "klima"=>"0" '
Is it possible to convert this easily to hash without calling eval or
writing regexp?

eval is easy and unsecure. Regexp is not very simple if you want handle
every special case (nested values etc).

Mage

8 Answers

Tim Pease

8/6/2008 4:18:00 PM

0


On Aug 6, 2008, at 10:03 AM, Mage wrote:

> Hello,
>
> I have a string like: ' "gaz"=>"1", "viz"=>"1", "lift"=>"0",
> "kamra"=>"1", "klima"=>"0" '
> Is it possible to convert this easily to hash without calling eval
> or writing regexp?
>
> eval is easy and unsecure. Regexp is not very simple if you want
> handle every special case (nested values etc).
>
> Mage
>


str = ' "gaz"=>"1", "viz"=>"1", "lift"=>"0", "kamra"=>"1",
"klima"=>"0" '

h = {}
str.split(',').each do |substr|
ary = substr.strip.split('=>')
h[ary.first.tr('"','')] = ary.last.tr('"','')
end


But that won't handle nested values -- that's left for someone else
with more time than me.

Blessings,
TwP

Mage

8/6/2008 7:19:00 PM

0

Tim Pease wrote:
> str = ' "gaz"=>"1", "viz"=>"1", "lift"=>"0", "kamra"=>"1", "klima"=>"0" '
>
> h = {}
> str.split(',').each do |substr|
> ary = substr.strip.split('=>')
> h[ary.first.tr('"','')] = ary.last.tr('"','')
> end
>
>
> But that won't handle nested values -- that's left for someone else
> with more time than me.
Won't even work if any of the values contains a comma.

Mage

Maciej Tomaka

8/7/2008 2:21:00 PM

0

Mage wrote:
> Tim Pease wrote:
>> with more time than me.
> Won't even work if any of the values contains a comma.
>
> Mage

Why don't you use YAML?

>> { :qwe => "asd"}.to_yaml
=> "--- \n:qwe: asd\n"

>> YAML.load(({ :qwe => "asd"}.to_yaml))
=> {:qwe=>"asd"}


It works for most of things.
--
Posted via http://www.ruby-....

Mage

8/8/2008 8:55:00 PM

0

Maciej Tomaka wrote:
>
> Why don't you use YAML?
>
The input string isn't generated by me, and I cannot change its format.

Mage

Avdi Grimm

8/8/2008 9:39:00 PM

0

On Wed, Aug 6, 2008 at 12:03 PM, Mage <mage@mage.hu> wrote:
> eval is easy and unsecure. Regexp is not very simple if you want handle
> every special case (nested values etc).

You could try eval with a high SAFE level. Or write a quick parser
using TreeTop.

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Michael Morin

8/8/2008 10:31:00 PM

0

Mage wrote:
> Hello,
>
> I have a string like: ' "gaz"=>"1", "viz"=>"1", "lift"=>"0",
> "kamra"=>"1", "klima"=>"0" '
> Is it possible to convert this easily to hash without calling eval or
> writing regexp?
>
> eval is easy and unsecure. Regexp is not very simple if you want handle
> every special case (nested values etc).
>
> Mage
>

You could do something like this:

#!/usr/bin/env ruby
# UziMonkey <uzimonkey@gmail.com>

str = %q{"gaz"=>"1", "viz"=>"1", "lift"=>"0", "kamra"=>"1","klima"=>"0"}

hash = Hash[*
str.
split(/, +/).
map {|s|
s.match( /"([^"]+)"=>"([^"]+)"/ )[1,2]
}.
flatten
]

puts hash.inspect

But the regex makes a lot of assumptions. If the format of the hash
changes at all (for example, quotes removed or changed to single
quotes), it will break. If not, it should work fine.

--
Michael Morin
Guide to Ruby
http://ruby....
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company

Mage

8/9/2008 12:10:00 AM

0

Michael Morin wrote:
> You could do something like this:
>
> #!/usr/bin/env ruby
> # UziMonkey <uzimonkey@gmail.com>
>
> str = %q{"gaz"=>"1", "viz"=>"1", "lift"=>"0", "kamra"=>"1","klima"=>"0"}
>
> hash = Hash[*
> str.
> split(/, +/).
> map {|s|
> s.match( /"([^"]+)"=>"([^"]+)"/ )[1,2]
> }.
> flatten
> ]
>
> puts hash.inspect
>
> But the regex makes a lot of assumptions. If the format of the hash
> changes at all (for example, quotes removed or changed to single
> quotes), it will break. If not, it should work fine.
>

It won't work if any if the values has a comma, like str = ' "gaz" =>
"1", "lift" =>"white, small"'

split(/, +/) will break "white, small"

A much more complicated regexp should work. As far as I see I will stay
with eval.

Mage


Maciej Tomaka

8/9/2008 2:02:00 PM

0

Mage wrote:
> Michael Morin wrote:
>> map {|s|
>>
> It won't work if any if the values has a comma, like str = ' "gaz" =>
> "1", "lift" =>"white, small"'
>
> split(/, +/) will break "white, small"
>
> A much more complicated regexp should work. As far as I see I will stay
> with eval.
>
> Mage

s = str.match(/^\s*\{\s*(.+)\}\s*$/).captures.first;
Hash[*s.scan(/\"([^"]*)\"\s*=>\s*\"([^"]*)\"/).flatten]

Lets assume that each hash is in form:
{ "something" => "someting elswe"[, *] }

Escaped " in strings are not supported here.
--
Posted via http://www.ruby-....