From: "David A. Black" Date: 2005-12-01T09:03:21+09:00 Subject: Re: Help constructing interesting hash? Hi -- On Thu, 1 Dec 2005, Chris McMahon wrote: > Suppose I have an array of arrays like > > [[A, B, C][1,2,3]] > > I can easily make a hash (using each_with_index) where one value is > the lookup value for the other value: > > 1=>A > 2=>B > 3=>C > > Now suppose I have a AoA like > > [[A, B, C,][1,2,2]] > > Is there a readable way to construct a hash like > > 1=>[A] > 2=>[B, C] Here's an inject-based way: a.zip(b).inject({}) do |hash,(key,value)| (hash[value] ||= []) << key hash end David -- David A. Black dblack@wobblini.net