Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save ta1kt0me/ecf1ede307d6a93a90a7 to your computer and use it in GitHub Desktop.

Select an option

Save ta1kt0me/ecf1ede307d6a93a90a7 to your computer and use it in GitHub Desktop.
CSVのデータ行の合計を出力する
###```csv test.csv
# j,m,h,s
# 1,2,3,4
# 5,6,7,8
# 9,0,1,2
###```
data = CSV.open("test.csv","r")
header = data.take(1)[0]
calc = Array.new(header.size).map{[]}
data.each {|v| v.each_with_index {|v, i| calc[i] << v }}
body=[]
calc.each {|col| body << col.map{|v| v.to_i}.inject(:+);}
puts "#{header.join(',')}¥n#{body.join(',')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment