Forum: Ruby Advanced array merging

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.
D877dec36ab3027ff7c9016e83cc1a4f?d=identicon&s=25 Jack Bauer (realmadrid2727)
on 2009-04-29 19:37
Hi everyone,

I'm looking for the fastest way (or the version with the least code!) to
consolidate a couple of arrays.

Basically, what a dump of what I have is this:


[[#<Ifinoctets_26 id: 2476, dtime: "2009-04-28 00:00:34", counter:
675392602>, #<Ifinoctets_26 id: 2476, dtime: "2009-04-28 00:05:34",
counter: 652224867>, #<Ifinoctets_26 id: 2476, dtime: "2009-04-28
00:10:34", counter: 637505981>, #<Ifinoctets_26 id: 2476, dtime:
"2009-04-28 00:15:34", counter: 813981324>, #<Ifinoctets_26 id: 2476,
dtime: "2009-04-28 00:20:34", counter: 753826918>],

[#<Ifinoctets_26 id: 2477, dtime: "2009-04-28 00:00:37", counter:
287888720>, #<Ifinoctets_26 id: 2477, dtime: "2009-04-28 00:05:37",
counter: 260326966>, #<Ifinoctets_26 id: 2477, dtime: "2009-04-28
00:10:37", counter: 287575144>, #<Ifinoctets_26 id: 2477, dtime:
"2009-04-28 00:15:37", counter: 341606600>, #<Ifinoctets_26 id: 2477,
dtime: "2009-04-28 00:20:37", counter: 305102837>],

[#<Ifinoctets_28 id: 2484, dtime: "2009-04-28 00:00:36", counter:
2976043831>, #<Ifinoctets_28 id: 2484, dtime: "2009-04-28 00:05:36",
counter: 2904676029>, #<Ifinoctets_28 id: 2484, dtime: "2009-04-28
00:10:36", counter: 2901969681>, #<Ifinoctets_28 id: 2484, dtime:
"2009-04-28 00:15:36", counter: 2827196523>, #<Ifinoctets_28 id: 2484,
dtime: "2009-04-28 00:20:36", counter: 2532538201>]]


I need to be able to grab the first record of each array (that happens
to be within the outer array) and merge them into one, keeping the
class, ID, and dtime of the first of the three and the SUMMED counter of
all three. In reality there could be anywhere between 2 and 30 arrays in
there each with any number of records. So let's say we have:

#<Ifinoctets_1 id: 100, dtime: "2009-04-28 00:00:34", counter: 500>
+
#<Ifinoctets_2 id: 111, dtime: "2009-04-28 00:00:37", counter: 1000>
+
#<Ifinoctets_3 id: 122, dtime: "2009-04-28 00:00:36", counter: 500>

=

#<Ifinoctets_1 id: 100, dtime: "2009-04-28 00:00:34", counter: 2000>
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-04-29 20:10
(Received via mailing list)
On Wed, Apr 29, 2009 at 11:07 PM, Jack Bauer <realmadrid2727@yahoo.es>
wrote:
> counter: 652224867>, #<Ifinoctets_26 id: 2476, dtime: "2009-04-28
>
> class, ID, and dtime of the first of the three and the SUMMED counter of
>
> #<Ifinoctets_1 id: 100, dtime: "2009-04-28 00:00:34", counter: 2000>

pseudocode:

def add_octets(x, y)
  ret = x.dup
  ret.counter += y.counter
  ret
end

ary.inject {|e,i| add_octets(e,i)}

martin
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-30 10:37
Jack Bauer wrote:
> Hi everyone,
>
> I'm looking for the fastest way (or the version with the least code!) to
> consolidate a couple of arrays.
>

Those two requirements are often at odds with each other.  For instance,
"inject" and "fastest" should not be used in the same sentence.

result = []

arr.each do |sub_arr|
  counter_total = 0

  sub_arr.each do |obj|
    counter_total += obj.counter
  end

  sub_arr[0].counter = counter_total
  result << sub_arr[0]   #or result << [sub_arr[0]]
end
D877dec36ab3027ff7c9016e83cc1a4f?d=identicon&s=25 Jack Bauer (realmadrid2727)
on 2009-04-30 16:22
7stud -- wrote:
>
> Those two requirements are often at odds with each other.  For instance,
> "inject" and "fastest" should not be used in the same sentence.
>
> result = []
>
> arr.each do |sub_arr|
>   counter_total = 0
>
>   sub_arr.each do |obj|
>     counter_total += obj.counter
>   end
>
>   sub_arr[0].counter = counter_total
>   result << sub_arr[0]   #or result << [sub_arr[0]]
> end


Thanks, 7stud. I forgot to mention in my description that after it did
the sum of the first item in each of the 3 subarrays to move on and do
all the second items, then the third, etc. Using yours as a base I ended
up with:

index = 0
array[0].length.times do
  counter_total = 0
  array.each do |sub_array|
    counter_total += sub_array[index].counter
  end
  array[0][index].counter = counter_total
  data_stream_out << array[0][index]
  index += 1
end

Works like a charm and enormously faster than what I was doing before I
posted this thread. Before I asked for help here I was using inject and
the calculation was taking 28 seconds to complete. This one took 4 and
there were about 2,000 more records this time too.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-30 23:31
Jack Bauer wrote:
> Thanks, 7stud. I forgot to mention in my description that after it did
> the sum of the first item in each of the 3 subarrays
>

Whoops.  That's not what my code does--it sums the totals within one
array.  I did a vertical traversal rather than a horizontal traversal.

> move on and do
> all the second items, then the third, etc. Using yours as a base I ended
> up with:
>
> index = 0
> array[0].length.times do
>   counter_total = 0
>   array.each do |sub_array|
>     counter_total += sub_array[index].counter
>   end
>   array[0][index].counter = counter_total
>   data_stream_out << array[0][index]
>   index += 1
> end
>
> Works like a charm and enormously faster than what I was doing before I
> posted this thread. Before I asked for help here I was using inject and
> the calculation was taking 28 seconds to complete. This one took 4 and
> there were about 2,000 more records this time too.

I read your original post again and with all the talk of merging and
summing, I decided I have no idea what you are trying to do.  If you've
got what you want--great!
This topic is locked and can not be replied to.