From: Ross Bamford Date: 2005-12-01T07:22:31+09:00 Subject: Re: Help constructing interesting hash? On Wed, 30 Nov 2005 21:57:00 -0000, 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] > > ? > > Thanks, > -Chris > I assumed you meant [['A', 'B', 'C'],[1,2,2]] - I know comma at end can be valid syntax but not sure if it fits here? If so, this seems to work: a = [['A', 'B', 'C'],[1,2,2]] h = {} a[1].each_with_index { |it, i| (h[it] ||= []) << a[0][i] } p h # => {1=>["A"], 2=>["B", "C"]} -- Ross Bamford - rosco@roscopeco.remove.co.uk