splitting an array into sub_arrays ...need advice

J

Josselin

I have an array similar to this example (after sorting it on a[1]
and a[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[1] and a[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[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[3] = 0
end

then, I wrote this method

def count_markers(a)
n = 0
0.step(a.nitems-2, 1) do |i|
if (a[1] == a[i+1][1]) && (a[2] == a[i+1][2])
n = n+1
a[3] = n
else
a[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
 
O

Olivier Renaud

Le dimanche 25 mars 2007 17:50, Josselin a =E9crit=A0:
I have an array similar to this example (after sorting it on a[1]
and a[2])

start_array =3D [ [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[1] and a[2] ,
to get another array like this one :

resulting_array =3D [ [[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=
=20
=46acets 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]}
=3D> {[1, 2]=3D>[[4, 1, 2, 0], [5, 1, 2, 0]], [2, 1]=3D>[[6, 2, 1, 0], [7, =
2, 1,=20
0]], [1, 1]=3D>[[1, 1, 1, 0], [2, 1, 1, 0], [3, 1, 1, 0]], [2, 3]=3D>[[10, =
2, 3,=20
0]], [2, 2]=3D>[[8, 2, 2, 0], [9, 2, 2, 0]]}

It may help you for what you are trying to do.

=2D-=20
Olivier Renaud
 
J

Josselin

Le dimanche 25 mars 2007 17:50, Josselin a écrit :
I have an array similar to this example (after sorting it on a[1]
and a[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[1] and a[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.


thanks a lot I'll have a look asap
 
O

Olivier Renaud

Note that partition_by is a simple method with no dependencies. So, you can
just copy/paste this method in your code from facets.rubyforge.org online
documentation. Here it is :

module Enumerable
def partition_by
r = Hash.new{ |h,k| h[k]=[] }
each do |e|
r[ yield(e) ] << e
end
return r
end
end

And then :

require 'enumerator'
start_array.partition_by {|ary| ary[1..2]}.to_enum:)each_value).to_a
 
J

Josselin

Note that partition_by is a simple method with no dependencies. So, you can
just copy/paste this method in your code from facets.rubyforge.org online
documentation. Here it is :

module Enumerable
def partition_by
r = Hash.new{ |h,k| h[k]=[] }
each do |e|
r[ yield(e) ] << e
end
return r
end
end

And then :

require 'enumerator'
start_array.partition_by {|ary| ary[1..2]}.to_enum:)each_value).to_a

thanks a lot I got it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top