編寫一個接受任意大小的多維容器並將其轉換爲一維關聯數組的函數,該數組的鍵是表示其值的路徑的字符串原來的容器。任意大小的多維容器並將其轉換爲一維關聯數組
所以{ 'one' => {'two' => 3, 'four' => [ 5,6,7]}, 'eight'=> {'nine'=> {'ten'=>11}}}
將成爲 : "{'one/two' => 3,'one/four/0' => 5, 'one/four/1' => 6, 'one/four/2' => 7, 'eight/nine/ten' : 11}"
到目前爲止,我已經得到了這個...但我有一個很大的問題。任何指向我忽略的東西的指針?
def oneDimHash(hash)
if hash.is_a?(Fixnum)
puts "AHHH"
else
hash.each_pair do |key,value|
if value.is_a?(Hash)
@temp_key << key << '/'
oneDimHash(value)
elsif value.is_a?(Array)
value.each_with_index do |val,index|
puts index
@temp_key << "#{index}"
oneDimHash(val)
end
else
@temp_key << key
@result["#{@temp_key}"] = "#{value}"
@temp_key = ''
end
end
end
end
小心描述一下你想克服的問題嗎? – 2014-10-07 19:55:36