From: RichardOnRails Date: 2009-12-12T00:40:06+09:00 Subject: Re: Fill gaps in Array of Hashes On Dec 10, 1:56 pm, Aldric Giacomoni wrote: > Joao Silva wrote: > > Aldric Giacomoni wrote: > >> Joao Silva wrote: > >>> I have the following object as a result from MySQL: > > >>> [{:positive=>"0", :neutral=>"2", :total=>"5", :date=>"2009-12-01", > >>> :negative=>"3"}, {:positive=>"3", :neutral=>"3", :total=>"7", > >>> :date=>"2009-12-08", :negative=>"1"}, {:positive=>"1", :neutral=>"1", > >>> :total=>"3", :date=>"2009-12-09", :negative=>"1"}] > > >>> I want to fill gaps (for example from 2009-11-19 till 2010-10-01) with > >>> {:positive=>"0", :neutral=>"0", :total=>"0", :date=>"HEREDATE", > >>> :negative=>"1" - how i can do this in ruby? In MySQL it's impossible to > >>> do :-(. > > >> Alright... Now that I've helped you do what you asked for, please tell > >> us what you actually want to do, because actually filling up a hash with > >> default data is really not optimal. > > > I don't have choice: I need data formed like this to generate report > > (chart) data. And i need for each day approporiate values. > > You could check whether the data is in the array, and return the default > that you want if it's not in there. > > Pseudo code: > > From date1 to date2, do |date| >  data = array.find { |x| x[:date] == date } >  if data.nil? >    data = { your default hash } >  end > >  do_whatever_you_want_with_data_here > end > > Why do you have an array of hashes, anyway? > Why not a hash with the dates as keys and the rest of the hash as > values? a hash of hashes. > -- > Posted viahttp://www.ruby-forum.com/. Maybe this is what the O.P. wants: # Aldric Giacomoni's neat solution, modified slightly require 'rubygems' require 'activesupport' # What? I'm lazy. to_date is damn handy. array = [ {:positive=>"0", :neutral=>"2", :total=>"5", :date=>"2009-12-01", :negative=>"3"}, {:positive=>"3", :neutral=>"3", :total=>"7", :date=>"2009-12-09", :negative=>"1"}, {:positive=>"1", :neutral=>"1", :total=>"3", :date=>"2009-12-05", :negative=>"1"} ] default = {:positive=>"0", :neutral=>"0", :total=>"0", :date=>"HEREDATE", :negative=>"1"} range = ("2009-12-01".to_date .. "2009.12.11".to_date) range.each { |date| next if array.find { |row| row[:date].to_date == date.to_date } array << default = {:positive=>"0", :neutral=>"0", :total=>"0", :date=>"# {date.to_s}", :negative=>"1"} } array.sort { |r1, r2| r1[:date] <=> r2[:date]}.each { |row| puts row.inspect} ======= Result ======= {:neutral=>"2", :total=>"5", :date=>"2009-12-01", :negative=>"3", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-02", :negative=>"1", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-03", :negative=>"1", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-04", :negative=>"1", :positive=>"0"} {:neutral=>"1", :total=>"3", :date=>"2009-12-05", :negative=>"1", :positive=>"1"} {:neutral=>"0", :total=>"0", :date=>"2009-12-06", :negative=>"1", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-07", :negative=>"1", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-08", :negative=>"1", :positive=>"0"} {:neutral=>"3", :total=>"7", :date=>"2009-12-09", :negative=>"1", :positive=>"3"} {:neutral=>"0", :total=>"0", :date=>"2009-12-10", :negative=>"1", :positive=>"0"} {:neutral=>"0", :total=>"0", :date=>"2009-12-11", :negative=>"1", :positive=>"0"} HTH, Richard