```Hi

On Thu, Mar 18, 2010 at 11:50 PM, Steve Wilhelm <steve / studio831.com> wrote:
> I have an array of records that contain timestamps at random intervals.
> The records are ordered by timestamp.
>
> I would like to convert the array into an array of arrays; each subarray
> would contain "grouped records." Grouping would occur if the timestamp
> of the next element in the original array is within thirty seconds of
> the current element.
>
> Example (second column is timestamp in seconds starting from zero).
>
> A 0
> B 15
> C 35
> D 100
> E 205
> F 215
> G 300
>
> would result in
>
> [[A, B, C], [D], [E, F], [G]]
>
> Any help on how to do this in the "Ruby Way" would be appreciated.

1 arr = [0,15,35,100,205,300]
2 arr2 = [0, 20, 40]
3
4 def group(array)
5   array.map!{|e| [e]}
6   array.inject([]) do |r, e|
7     if r == [] or e[0] - r.last.last > 30 then
8       r.push(e)
9     else
10       r[-1].push(e[0])
11     end
12       r
13   end
14 end
15
16 puts arr.inspect
17 puts group(arr).inspect
18 puts arr2.inspect
19 puts group(arr2).inspect

--
Roger Braun
http://yononaka.de
roger.braun / student.uni-tuebingen.de

```