Hi guys, i’m having difficulty building the algorithm on my little
problem.
I got this sample csv
Month, Revenue
January 2014, $1234
January 2014, $3342
January 2014, $974
February 2014, $552
February 2014, $5923
February 2014, $581
February 2014, $5814
February 2014, $842
February 2014, $912
March 2014, $744
March 2014, $1123
March 2014, $152
============
My goal is to summarize the month with the total revenue, my first
solution is like to store the previous month of the row then if it’s the
same then still add to the previous row.
The result is gonna be like
January 2014, $4534
February 2014, $2343
March 2014, $2343
Help me out here a little 
Arman Jon Villalobos wrote in post #1152134:
Hi guys, i’m having difficulty building the algorithm on my little
problem.
I got this sample csv
Month, Revenue
January 2014, $1234
January 2014, $3342
January 2014, $974
February 2014, $552
February 2014, $5923
February 2014, $581
February 2014, $5814
February 2014, $842
February 2014, $912
March 2014, $744
March 2014, $1123
March 2014, $152
The result is gonna be like
January 2014, $4534
February 2014, $2343
March 2014, $2343
Something like that:
h= Hash.new {|k,h| h[k]=0}
File.open(“filename.csv”) { |f| f.each_line {|line|
a=line.split("\t")
h[a.first.strip]+= h[a.last.strip[1…-1].to_i
} }
p h
Hello Sir,
I’ve tried your suggestion even though some of it doesn’t make sense to
me since i’m beginner, the error is :
`[]=’: no implicit conversion of Hash into Integer (TypeError)
that’s on the
h= Hash.new {|k,h| h[k]=0}.
I know about blocks, but I can’t seem to understand the error.
Try reversing the parameter order in the block, i.e., use |h, k| instead
of |k, h|
The syntax is Hash.new { |hash_object, key_object|
hash_object[key_object] = 0}
It doesn’t matter what you name them, you can use |a, b| or whatever.
You just have to be consistent.