From: Aldric Giacomoni Date: 2009-12-18T02:29:49+09:00 Subject: Re: Unique combination of values from arrays Milo Thurston wrote: > > The purpose is so that I can find all the unique combinations of > parameters for different fields in SQL queries for some data I have. Well, I shamelessly stole and adapted the code from there: http://stackoverflow.com/questions/710670/c-permutation-of-an-array-of-arraylists def array_permutations index, array result = [] if index == array.size result << "" return result end array[index].each do |element| array_permutations(index + 1, array).each do |x| result << "#{element}, #{x}" end end return result end It's a bit ugly, but it'll pretty much do what you want. Let me know if you can come up with a better variable name than 'x' ;-) irb(main):051:0> one = ['one', 'two'] => ["one", "two"] irb(main):052:0> two = ['three', 'four'] => ["three", "four"] irb(main):053:0> three = ['five', 'six'] => ["five", "six"] irb(main):054:0> a = array_permutations 0, [one, two, three] => ["one, three, five, ", "one, three, six, ", "one, four, five, ", "one, four, six, ", "two, three, five, ", "two, three, six, ", "two, four, five, ", "two, four, six, "] -- Posted via http://www.ruby-forum.com/.