# Asking for help with operating on data

**URL:** <https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079>\
**Category:** Rails\
**Created:** [May 29, 2009, 4:20am UTC](https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079 "2009-05-29T04:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 29, 2009, 4:20am UTC](https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079/1 "2009-05-29T04:20:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 29, 2009, 5:27am UTC](https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079/2 "2009-05-29T05:27:22Z")

</div>

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  
> […]  
> 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?

Well, some ideas:

- Get the whole dataset (or each batch of 3000) into memory with one  
query before operating. No sense in making the database bottleneck any  
worse than it has to be.

- Find a way to do the calculations on the DB side, using aggregate  
functions and/or (gasp!) stored procedures. This will probably be  
easier if you’re using PostgreSQL or Oracle, since mySQL’s procedural  
language is so weak as to be unusable for non-trivial work.

- Provide more detail and we might be able to help more.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 30, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079/3 "2009-05-30T22:08:48Z")

</div>

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 W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 30, 2009, 8:47am UTC](https://www.ruby-forum.com/t/asking-for-help-with-operating-on-data/169079/4 "2009-05-30T08:47:35Z")

</div>

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
