From: "William James" <> Date: 2009-12-18T03:40:34+09:00 Subject: Re: Unique combination of values from arrays Milo Thurston wrote: > Here's a problem for which I can't see an obvious solution, and hope > someone might have some suggestions. > > I have several arrays, each with a variety of text values in them. I > need to construct some strings consisting of each unique combination > of the arrays in order. Here's a very simple example: > > first_array = ['one','two'] > second_array = ['three','four'] > > required output: > > one,three > one,four > two,three > two,four > > There are more arrays than that and more entries in each array, but > that's the basic idea. Any advice welcome - thanks! one = [1,2] two = [3,4] three = [5,6,7] a = [one, two, three] def combi ary_of_ary ary_of_ary.inject([[]]){|old,lst| lst.inject([]){|new,e| new + old.map{|c| c + [ e ] }}} end puts combi( a ).map{|x| x.join ','}.sort === output === 1,3,5 1,3,6 1,3,7 1,4,5 1,4,6 1,4,7 2,3,5 2,3,6 2,3,7 2,4,5 2,4,6 2,4,7 --