Marnen - I'd rather do it in Ruby instead of on the database side. In
case my description was unclear, I'd like to iterate over an array,
and for each iteration to be able to access a value determined in the
previous iteration.

Wisccal - that looks like it's close to what I'm looking to do.
However, I don't understand the enumerable#each_with_object so I
probably need to do more reading in my ruby book.

On May 30, 2:47 am, Wisccal Wisccal <rails-mailing-l...@andreas-s.net>
wrote:
> eggman2001 wrote:
> > I have a few thousand rows of stock market data - one row for each
> > day. I'm using ActiveRecord to perform database operations.
>
> > I'm interested in performing a calculation on each row while
> > incorporating the result of the calculation on the previous row, and
> > then once the calculation has been performed for all rows, perform a
> > new calculation that uses the individual calculations of each row.
>
> > I figure that I'll start with an array of size 3,000. Then I would
> > probably want to iterate over it, possibly saving the result of the
> > operation of each row to a new array but this area is a little foggy.
>
> > Any suggestions?
>
> Assuming you want to calculate something like percent change for each
> day, you could load the entire result set into an array, and then do
> something like:
>
> changes = @stock_data.each_with_object([]) do |data, array|
>  last_value = array.last.value
>  change = (data.value - last_value) / last_value * 100 unless
> last_value.zero?
>  array << [data.transaction_date, change]
> end
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to