On Fri, Sep 25, 2009 at 4:52 PM, Paul Smith <paul / pollyandpaul.co.uk> wrote:
> On Fri, Sep 25, 2009 at 4:39 PM, Paul Smith <paul / pollyandpaul.co.uk> wrote:
>> On Fri, Sep 25, 2009 at 4:34 PM, Li Chen <chen_li3 / yahoo.com> wrote:
>>> Hi all,
>>>
>>> I have a 2D array as follow:
>>>
>>> arr=[
>>>
>>> [ 'a',1,2,3],
>>> ['b',4,5,6],
>>> ['c',3,2,1],
>>> ['a',1.3,2.2,3,3],
>>> ...
>>> ['a', 2,1,3],
>>> ...
>>> ['a',2.1,1.5,3]
>>>
>>> ]
>>>
>>> As you can see there are several rows containing   ᧬
>>> and the last row containing 'a'' is the lastest one.     
>>> is possible to return a new array which contains the latest row having
>>> 'a' ,
>>> together with other rows.
>>
>> result = []
>> arr.each { |x| result = x if x[0] == 'a' }
>> result
>>
>> ?
>
> Sorry, this only solves the "Last row containing a" part of the
> problem.     
> first element as the key to a hash.
>
> result = {}
> arr.each{|x| result[x[0]] = x[1,x.length-1]}
>

I knew there was a a better way to do this:

arr.each{|x| result[x[0]] = x[1..-1]}

is a more succinct second line.

-- 
Paul Smith
http://www.nomadicfun.co.uk

paul / pollyandpaul.co.uk