From: Ne Scripter Date: 2009-09-30T00:28:42+09:00 Subject: Re: Group by unique entries of a hash Ah yes, that makes perfect sense. Thanks Paul Smith wrote: > > You want a hash where the key is the element you want to group on, and > the 'item' is an array of all items with the shared key. A bit like > (untested): > > hashMapping = {} > > IO.foreach(source.to_s) do |data| > fields = data.split(",") > hash = fields[0] > ocrID = fields[1] > > hashMapping[ocrID] ||= [] #If hashMapping has never seen this key > before, make an empty array > > hashMapping[ocrID] << hash #Add the new element to the array for this > key > > end > >> >> Many thanks >> -- >> Posted via http://www.ruby-forum.com/. >> >> > -- > Paul Smith > http://www.nomadicfun.co.uk > > paul@pollyandpaul.co.uk -- Posted via http://www.ruby-forum.com/.