From: Olivier Renaud Date: 2007-03-26T02:00:00+09:00 Subject: Re: splitting an array into sub_arrays ...need advice Le dimanche 25 mars 2007 17:50, Josselin a �crit�: > I have an array similar to this example (after sorting it on a[i][1] > and a[i][2]) > > start_array = [ [1, 1, 1, 0], [2, 1, 1, 0], [3, 1, 1, 0], [4, 1, 2, 0], > [5, 1, 2, 0], [6, 2, 1, 0], [7, 2, 1, 0], [8, 2, 2, 0], [9, 2, 2, 0], > [10, 2, 3, 0] ] > > I am trying to split it, into sub_arrays according to similarelements > a[i][1] and a[i][2] , > to get another array like this one : > > resulting_array = [ [[1, 1, 1, 0], [2, 1, 1, 0], [3, 1, 1, 0]] , > [[4, 1, 2, 0], [5, 1, 2, 0]] , [[6, 2, 1, 0], [7, 2, 1, 0]] , [[8, > 2, 2, 1], [9, 2, 2, 0]] , [[10, 2, 3, 0]] ] There is a way to group objects easily with Enumerable#partition_by, in the Facets gem. irb(main):001:0> require 'facets' irb(main):002:0> require 'enumerable/partition_by' irb(main):006:0> start_array.partition_by {|ary| ary[1..2]} => {[1, 2]=>[[4, 1, 2, 0], [5, 1, 2, 0]], [2, 1]=>[[6, 2, 1, 0], [7, 2, 1, 0]], [1, 1]=>[[1, 1, 1, 0], [2, 1, 1, 0], [3, 1, 1, 0]], [2, 3]=>[[10, 2, 3, 0]], [2, 2]=>[[8, 2, 2, 0], [9, 2, 2, 0]]} It may help you for what you are trying to do. -- Olivier Renaud