From: Daniel Bush Date: 2008-08-20T21:58:02+09:00 Subject: Re: array of hashes - need to iterate and calulate stats but how Adam Akhtar wrote: > hi lex, > > im not so hot on ruby, could you expand on what you mean by that? Hi Adam, I quite like the class and struct approaches already mentioned but I had a stab at just using plain arrays and hashes (not extensively tested): require 'pp' items = [ { :title => 'title1' , :model => '123a' , :sold => false }, { :title => 'title2' , :model => '123a' , :sold => false }, { :title => 'title3' , :model => '123b' , :sold => true }, { :title => 'title4' , :model => '123c' , :sold => false }, { :title => 'title5' , :model => '123c' , :sold => true } ] summary=Hash.new({:listings => 0 , :sold => 0 , :rate => 0}) items.each do |item| # Store default value if key in 'summary' doesn't exist. summary[item[:model]]=summary.default.clone unless \ summary.has_key?(item[:model]) model=summary[item[:model]] model[:listings]+=1 model[:sold]+=1 if item[:sold] model[:rate]=model[:sold].to_f/model[:listings].to_f end pp summary Regards, Daniel -- Posted via http://www.ruby-forum.com/.