From: Simon Strandgaard Date: 2005-10-22T05:09:17+09:00 Subject: Re: array in hash... argh!! On 10/21/05, Simon Strandgaard wrote: > On 10/21/05, Marco wrote: > [snip] > > and i wold like to get 3 hashes like tese: > > > > > h = { > "title"=>["ciccio", "test", "ciccio panza"], > "id"=>["3", "4", "1"], > "description"=>["1", "", "hey?"], > "insert_date"=>["2005-10-19", "2005-10-20", "2005-10-18"] > } > > vals = h.map{|k,v| v}.transpose > keys = h.map{|k,v| k} > ary_of_pairs = vals.map{|ary| keys.zip(ary)} > res = ary_of_pairs.map{|pairs| Hash[*pairs]} > puts res.map{|hash| hash.inspect }.join("\n") a bit shorter.. res = h.values.transpose.map{|ary| h.keys.zip(ary)}.map{|pairs| Hash[*pairs]} > =begin > output: > > {["title", "ciccio"]=>["id", "3"], ["description", > "1"]=>["insert_date", "2005-10-19"]} > {["description", ""]=>["insert_date", "2005-10-20"], ["title", > "test"]=>["id", "4"]} > {["title", "ciccio panza"]=>["id", "1"], ["description", > "hey?"]=>["insert_date", "2005-10-18"]} > > =end > > -- > Simon Strandgaard >