From: Martin DeMello Date: 2009-04-30T03:09:16+09:00 Subject: Re: Advanced array merging On Wed, Apr 29, 2009 at 11:07 PM, 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. > > Basically, what a dump of what I have is this: > > > [[# 675392602>, # counter: 652224867>, # 00:10:34", counter: 637505981>, # "2009-04-28 00:15:34", counter: 813981324>, # dtime: "2009-04-28 00:20:34", counter: 753826918>], > > [# 287888720>, # counter: 260326966>, # 00:10:37", counter: 287575144>, # "2009-04-28 00:15:37", counter: 341606600>, # dtime: "2009-04-28 00:20:37", counter: 305102837>], > > [# 2976043831>, # counter: 2904676029>, # 00:10:36", counter: 2901969681>, # "2009-04-28 00:15:36", counter: 2827196523>, # 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: > > # > + > # > + > # > > = > > # pseudocode: def add_octets(x, y) ret = x.dup ret.counter += y.counter ret end ary.inject {|e,i| add_octets(e,i)} martin