From: Dave Burt Date: 2005-10-12T13:56:55+09:00 Subject: Re: small array/hash question Kev Jackson: >>> loop across data here >>> work_types[nsc_id]=do |types| >>> types << data[7] >>> end >>> end loop >>> where work_types is the hash and types is the array I want to accumulate >>> data in > ... > I got the output I wanted with this > > work_types = Hash.new > if work_types.has_key?(nsc_id) then > work_types[nsc_id]= work_types[nsc_id].include?(work_type) ? > work_types[nsc_id] : work_types[nsc_id] << work_type > else > work_types[nsc_id]= [work_type] > end > > So the problem is solved, but I wonder if there's a more elegant way of > doing it (especially the check to see if the value is already in the > array). My first assumption was that assignment to a Hash took a block > (hence the pseudo code), I was actually a little suprised that it didn't > ;) How about this? work_types = Hash.new {|h, k| h[k] = [] } work_types[nsc_id] << work_type unless work_types[nsc_id].include?(work_type) Cheers, Dave