From: Josselin Date: 2007-03-26T00:50:10+09:00 Subject: splitting an array into sub_arrays ...need advice 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]] ] ----- herafter is how I proceed.. but I am not sure where I go.. is it not too complicated (not even saying DRY..) I am using a[i][3] as a counter, so I can change its value, before executing the splitting 0.step(start_array.nitems-1, 1) do |i| start_array[i][3] = 0 end then, I wrote this method def count_markers(a) n = 0 0.step(a.nitems-2, 1) do |i| if (a[i][1] == a[i+1][1]) && (a[i][2] == a[i+1][2]) n = n+1 a[i][3] = n else a[i][3] = n +1 n = 0 end end a[a.nitems-1][3] = 1 if a[a.nitems-1][3] == 0 end so I get a count of the number of elements to put in each sub-array counted_array = count_markers(start_array) which gives me : counted_array => [ [1, 1, 1, 1], [2, 1, 1, 2], [3, 1, 1, 3], [4, 1, 2, 1], [5, 1, 2, 2], [6, 2, 1, 1], [7, 2, 1, 2], [8, 2, 2, 1], [9, 2, 2, 2], [10, 2, 3, 1] ] now I don't see how to split it to get the resulting array 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]] ] where resulting_array.nitems = 5 resulting_array[0] = [[1, 1, 1, 0], [2, 1, 1, 0], [3, 1, 1, 0]] ... and so on