I have a csv which I would like to process instances of a row[report] based on the input of another row[number].
Report,Number,Name,Date
INC AMT %,12345678,ACME INC,10/30/2012
INC AMT %,12345678,ACME INC,10/31/2012
OUT AMT %,12345678,ACME INC,10/31/2012
# shortened for brevity
My code block looks like this:
require 'CSV'
module AlertHash
@alert_hash = {}
CSV.foreach("alerts.csv") do |row|
report, number = row
next if number == "Number"
@alert_hash[number] = report
end
def self.report(number)
@alert_hash[number]
end
end
puts "Alert: " + AlertHash.report("12345678")
# OUTPUT => Alert: OUT AMT %
# DESIRED OUTPUT => Alert: INC AMT %, OUT AMT %
I’ve been able to process the last [report]stack of each number. However, my desired output would be to capture ALL reports for a given row[number]. Can someone give me some pointers on how this could be done? Maybe I’m approaching this problem the wrong way.
This statement:
will overwrite any previous value for any given number index, so you only retain the last one. That’s why you get the
OUTvalue only. If you want to collect all lines for a given number, use an array:and then concatenate them in your
reportmethod:This will give you:
Not sure what you’re actually trying to achieve, but this explains your code’s current behaviour.