On 03/19/2010 03:33 PM, Glenn Jackman wrote:
> At 2010-03-18 06:50PM, "Steve Wilhelm" 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.
>
> Lots of verbose answers.  It can be quite short:
>
>     a = [['a',0],['b',15],['c',35],['d',100],['e',205],['f',215],['g',300]]
>
>     a.group_by {|name,val| val/100} .
>       collect do |key,list|
>         list.collect {|name, time| name}
>       end
>
>     # => [["a", "b", "c"], ["d"], ["e", "f"], ["g"]]
>

Yeah, but as far as I can see that's not a solution to the problem the
OP wanted to solve.  He wants to build chains based on the delta and not
based on the fact that they fall in the same interval.  Am I missing
something?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/