Created
July 23, 2013 16:05
-
-
Save lucacervasio/6063648 to your computer and use it in GitHub Desktop.
tree from dotted separated strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
a = { | |
'uno.due.tre.otto' => 2, | |
'uno.due.tre.sei' => 5, | |
'uno.due.quattro' => 'ciao', | |
'uno.sette.cinque' => 'true', | |
} | |
mib = {'uno' => {}} | |
a.each_pair do |item,val| | |
cur = mib | |
item.split(".").each_with_index do |part,index| | |
if cur.has_key?(part) | |
cur = cur[part] | |
else | |
if item.split(".").length - 1 == index | |
cur[part] = val | |
else | |
cur[part] = {} | |
end | |
cur = cur[part] | |
end | |
end | |
end | |
p mib | |
puts mib['uno']['due']['tre']['otto'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment