From: yermej Date: 2008-01-25T00:24:57+09:00 Subject: Re: Change a data structure On Jan 24, 9:06 am, Michael Schmarck wrote: > yermej wrote: > > I'll offer up an inject version (mostly for novelty's sake): > > timing_given.each {|k, v| timing_given[k] = v.inject(Hash.new {|h, k| > > h[k] = []}) {|acc, h| h.each {|k, v| acc[k] << v}; acc}} > > Geez - did you use to code perl code? Because Perl code tends to > be as unreadable and compact as yours :) Yes, I've used Perl in the past, but my penchant for inject is rooted in Scheme. > It works good, but I guess I've got some reading to do, to actually > understand why it's working ;-> This might help, though some would say it's better just to avoid inject entirely: timing_given.each do |test_type, test_data| timing_given[test_type] = test_data.inject(Hash.new {|h, k| h[k] = []}) do |new_hash, old_hash| old_hash.each do |data_field, data_value| new_hash[data_field] << data_value end new_hash end end