I am trying to do some math on a section of an array. And then I want to increment to do math on more of it. However I don’t know how to do it correctly.
The below code simply puts 3 columns of csv data into 1 array each. And then does math on it.
require 'csv'
csv_data = 'data.csv'
Location = []
CSV.foreach(csv_data,'r') {|row| Location << row[0]}
Data1 = []
CSV.foreach(csv_data,'r') {|row| Data1 << row[1]}
Data2 = []
CSV.foreach(csv_data,'r') {|row| Data2 << row[2]}
index = 500
indexmax = 510
while index < indexmax
MathResults = Data1[index].to_f - Data2[index-1].to_f
index += 1
puts MathResults
end
If I put the MathResults above the while loop I obviously get the same result 10 times. If I put it in the loop I get 10 different results like I want but lots of errors with it that say:
warning: already initialized constant MathResults
I know this is still usable but the errors must be telling me I should be doing things differently.
So I was wondering if someone could tell me.
Thank you!
Edit: To add one thing. I realize I have 3 arrays and only 2 in use. The Location array is to later fill out the index variable after searching for the correct location. Instead of it saying just 500. Don’t worry about that.
You should probably use plain variable instead of constant (math_results instead of MathResults).