From: Belorion Date: 2005-12-01T08:05:56+09:00 Subject: Re: Help constructing interesting hash? ------=_Part_41394_7580586.1133391949798 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Here's a construct I use quite frequently: hsh =3D Hash.new{ |hh,kk| hh[kk] =3D Array.new } What this does is that the default value of hsh[somekey] is a new array. So, with that, we can do this: a =3D [['a', 'b', 'c'],[1,2,2]] hsh =3D Hash.new{ |hh,kk| hh[kk] =3D Array.new } a[0].each_index{ |ii| hsh[ a[1][ii] ].push( a[0][ii] ) } hsh =3D> {1=3D>["a"], 2=3D>["b", "c"]} On 11/30/05, Sean O'Halpin wrote: > > On 11/30/05, 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=3D>A > > 2=3D>B > > 3=3D>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=3D>[A] > > 2=3D>[B, C] > > > > ? > > > > Thanks, > > -Chris > > > > Here's another way: > > a =3D [['A', 'B', 'C'],[1,2,3]] > h =3D Hash[*a[0].zip(a[1]).flatten] > p h > -- OUTPUT -- > {"A"=3D>1, "B"=3D>2, "C"=3D>3} > > Regards, > > Sean > > ------=_Part_41394_7580586.1133391949798--