Forum: Ruby splitting an array into sub_arrays ...need advice

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-25 17:50
(Received via mailing list)
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
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-25 19:00
(Received via mailing list)
Le dimanche 25 mars 2007 17:50, Josselin a écrit :
>
> 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.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-25 22:01
(Received via mailing list)
On 2007-03-25 19:00:00 +0200, Olivier Renaud <o.renaud@laposte.net>
said:

>> to get another array like this one :
> 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
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-25 22:30
(Received via mailing list)
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
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-27 17:51
(Received via mailing list)
On 2007-03-25 22:28:42 +0200, Olivier Renaud <o.renaud@laposte.net>
said:

>     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
This topic is locked and can not be replied to.