From: Jeremy Wells Date: 2007-08-24T22:08:26+09:00 Subject: Re: Combining Array Elements gregarican wrote: > I have an array that I would like to combine elements. Here's a sample > array: > > [["Value A", "Value B", 3], ["Value A", "Value C", 2],["Value A", > Value B", 1]] > > What I would like to do is find the elements where the first two items > are the same and then combine the third. The resulting array would > consist of: > > [["Value A", "Value B", 4],["Value A", "Value C", 2]] > > This is something that is out in left field in terms of how I've used > arrays in the past. Anyone know of a quick bit of script I can whip up > that will suit the task? > new_array = array.collect{|v| [v[0],v[1], a.select{|b| b[0] == v[0] && b[1] == v[1] }.inject(0){|m,b| m + b.last}]}.uniq