字符串格式是这样的
"a.b.c = 1"
"a.b.d = 2"
=> its hash will be
=> {'a'=> {'b' => {'c'=>1, 'd'=>2 } } }
数组
会变得棘手 "a.e[0].f = 1"
"a.e[0].g = 2"
"a.e[1].h = 3"
=> its hash will be
=> {'a' => {'e' => [{'f'=>1, 'g'=>2}, {'h'=>3}] } }
我写了一个版本,它不处理带有太多if-else检查的数组
def construct
$output[$words[0]] = {} unless $output.has_key?($words[0])
pointer = $output[$words[0]]
$words[1..-2].each do |word|
pointer[word] = {} unless pointer.has_key?(word)
pointer = pointer[word]
end
pointer[$words[-1]] = 1
end
$output = {}
$words = ['a', 'b', 'c']
construct
$words = ['a', 'b', 'd']
construct
p $output
阵列版本更糟糕。有没有更好的方法在Ruby中解决这个问题?
答案 0 :(得分:3)
不像我预期的那么简单。以下是我提出的建议:
class Hash
def hash_merge(other)
update(other) do | key, val_self, val_other |
case val_self
when Hash
val_self.hash_merge val_other
when Array
val_self += [nil] * (val_other.length - val_self.length)
val_self.zip(val_other).map { | a, b | a && b ? a.hash_merge(b) : a || b }
else
# error
end
end
end
end
# parses a string of the form "a.e[1].g = 2" to a hash {"a" => {"e" => [nil , {"g" => 2}]}}
def construct_string(s)
if s =~ /\A\s*(\S+)\s+=\s+(\d+)\z/
s1, s2 = $1, $2
s1.split('.').reverse.inject(s2.to_i) do | hash, name |
if name =~ /\[(\d+)\]\z/
name, idx = $~.pre_match, $1.to_i
array = []
array[idx] = hash
{ name => array }
else
{ name => hash }
end
end
else
# error
end
end
# parses an array of string and merges the resulting hashes
def construct(s)
s.map { | e | construct_string(e) }.inject(&:hash_merge)
end
ins = ["a.e[0].f = 1",
"a.e[0].g = 2",
"a.e[1].h = 3"]
p construct(ins) # {"a"=>{"e"=>[{"f"=>1, "g"=>2}, {"h"=>3}]}}