I''ve been hacking on this for awhile and am not even getting close. Consider the following... results = Sample.find(:all) total_counter = results.each{|result| #Add together result.counter} I just want to simply iterate through the list and sum the counter field for each row into an aggregation. I could do it in a full for loop, but is there a nice one liner I''m missing? Thanks, Jeff
On Aug 23, 2005, at 10:14 AM, Jeff Casimir wrote:> I''ve been hacking on this for awhile and am not even getting close. > Consider the following... > > results = Sample.find(:all) > total_counter = results.each{|result| #Add together result.counter} > > I just want to simply iterate through the list and sum the counter > field for each row into an aggregation. I could do it in a full for > loop, but is there a nice one liner I''m missing? > > Thanks, > JeffYou need to use inject for this: total = results.inject(0) {|sum, result| sum + result.counter} -Scott
On 8/23/05, Jeff Casimir <jeff-+RlNNtFrnNmT15sufhRIGw@public.gmane.org> wrote:> results = Sample.find(:all) > total_counter = results.each{|result| #Add together result.counter} > > I just want to simply iterate through the list and sum the counter field > for each row into an aggregation. I could do it in a full for loop, but > is there a nice one liner I''m missing?try this total_count = results.inject(0) { |sum, sample| sum + sample.counter } Untested, but should work like this... Sebastian
On 8/23/05, Jeff Casimir <jeff-+RlNNtFrnNmT15sufhRIGw@public.gmane.org> wrote:> I''ve been hacking on this for awhile and am not even getting close. > Consider the following... > > results = Sample.find(:all) > total_counter = results.each{|result| #Add together result.counter} > > I just want to simply iterate through the list and sum the counter field > for each row into an aggregation. I could do it in a full for loop, but > is there a nice one liner I''m missing? > > Thanks, > Jeffresults_sum = results.inject(0) { |sum, result| sum + result.counter } #inject wass probably one of the hardest methods for me to learn. http://ruby-doc.org/core/classes/Enumerable.html#M001865 Just take a close look at all the array and enumerable methods. -- rick http://techno-weenie.net
Jeff Casimir wrote:> I''ve been hacking on this for awhile and am not even getting close. > Consider the following... > > results = Sample.find(:all) > total_counter = results.each{|result| #Add together result.counter} > > I just want to simply iterate through the list and sum the counter field > for each row into an aggregation. I could do it in a full for loop, but > is there a nice one liner I''m missing? > > Thanks, > JeffNot 100% sure about this but think it is what you want... total_counter = results.collect { |result| total_counter += result.counter } However, it seems it might be better to run a simple sql query for what you are trying to do.. "select sum(counter) from table_name" Chris
Right: irb(main):001:0> total = 0 => 0 irb(main):002:0> results = [1,2,3,4,5,6] => [1, 2, 3, 4, 5, 6] irb(main):003:0> total = results.inject(0) { |sum, res| sum + res } => 21 Wrong: irb(main):001:0> total = 0 => 0 irb(main):002:0> results = [1,2,3,4,5,6] => [1, 2, 3, 4, 5, 6] irb(main):003:0> total = results.collect { | res | total += res } => [1, 3, 6, 10, 15, 21] I would have guessed that both would exhibit the same behavior. Color me confused. On 8/23/05, Chris Roos <chris-zoUjy1rb4AnQXOPxS62xeg@public.gmane.org> wrote:> Jeff Casimir wrote: > > I''ve been hacking on this for awhile and am not even getting close. > > Consider the following... > > > > results = Sample.find(:all) > > total_counter = results.each{|result| #Add together result.counter} > > > > I just want to simply iterate through the list and sum the counter field > > for each row into an aggregation. I could do it in a full for loop, but > > is there a nice one liner I''m missing? > > > > Thanks, > > Jeff > > Not 100% sure about this but think it is what you want... > > total_counter = results.collect { |result| total_counter += result.counter } > > However, it seems it might be better to run a simple sql query for what > you are trying to do.. > > "select sum(counter) from table_name" > > Chris > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
Jeff Casimir wrote:> I''ve been hacking on this for awhile and am not even getting close. > Consider the following... > > results = Sample.find(:all) > total_counter = results.each{|result| #Add together result.counter} > > I just want to simply iterate through the list and sum the counter field > for each row into an aggregation. I could do it in a full for loop, but > is there a nice one liner I''m missing? > > Thanks, > Jeff > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >Sorry, my recent post was incorrect. You can still do either of these but I''d suggest Rick''s solution is ''better''. a = [1, 2, 3, 4, 5] b = 0; a.each { |val| b+=val } OR b = 0; a.collect { |val| b+=val } Sorry for the incorrect advice initially. Chris
On Aug 23, 2005, at 10:28 AM, Rick Olson wrote:>> > > results_sum = results.inject(0) { |sum, result| sum + result.counter } > > #inject wass probably one of the hardest methods for me to learn. > > http://ruby-doc.org/core/classes/Enumerable.html#M001865 > > Just take a close look at all the array and enumerable methods. > > > -- > rick > http://techno-weenie.net >Yeah, I often wish ruby had something like this (quick, off the top of my head hack): module Enumerable def sum(&block) if block_given? self.inject(0) { |s, x| s + yield(x) } else self.inject(0) { |s, x| s + x } end end end puts [x, y, z].sum { |i| i.val } puts [3, 6, 9].sum If you really want to grok some of this stuff I recommend learning a functional programming language. Haskell is pretty sweet and easy to pick up. -Scott
On Aug 23, 2005, at 10:29 AM, Chris Roos wrote:> Jeff Casimir wrote: >> I''ve been hacking on this for awhile and am not even getting close. >> Consider the following... >> results = Sample.find(:all) >> total_counter = results.each{|result| #Add together result.counter} >> I just want to simply iterate through the list and sum the counter >> field for each row into an aggregation. I could do it in a full for >> loop, but is there a nice one liner I''m missing? >> Thanks, >> Jeff > > Not 100% sure about this but think it is what you want... > > total_counter = results.collect { |result| total_counter += > result.counter } >Nope, #collect (aka #map) takes a collection and applies a block to each item and returns a collection of the results of those applications, so you can''t get a single value out of it (without perverting the concept entirely). -Scott
> Wrong: > > irb(main):001:0> total = 0 > => 0 > irb(main):002:0> results = [1,2,3,4,5,6] > => [1, 2, 3, 4, 5, 6] > irb(main):003:0> total = results.collect { | res | total += res } > => [1, 3, 6, 10, 15, 21]total = results.collect { | res | total += res }.last => 21 Wow fast moving thread. I had an explanation but Scott beat me to it. And my above solution possibly qualifies as "perverting the concept." -- rick http://techno-weenie.net
On Aug 23, 2005, at 11:12 AM, Rick Olson wrote:>> Wrong: >> >> irb(main):001:0> total = 0 >> => 0 >> irb(main):002:0> results = [1,2,3,4,5,6] >> => [1, 2, 3, 4, 5, 6] >> irb(main):003:0> total = results.collect { | res | total += res } >> => [1, 3, 6, 10, 15, 21] > > total = results.collect { | res | total += res }.last > => 21 > > Wow fast moving thread. I had an explanation but Scott beat me to it. > And my above solution possibly qualifies as "perverting the concept." > > > -- > rick > http://techno-weenie.netI give it a rating of XXX. ;) -Scott