result = [] ARGF.each do |line| lineno = ARGF.file.lineno - 1 result[lineno] ||= [] line.chomp.split(',').collect{|i|i.to_i}.each_with_index do |val, i| begin result[lineno][i] += val rescue NoMethodError result[lineno] << 0 retry end end end
csv ファイルを 10 個作ってベンチ取ってみた。 メモリ消費は8MBくらいかな。AthlonXP1700+ Win2KSP3 $ruby -v ruby 1.7.3 (2002-09-05) [i386-cygwin] $time test.sh real 1m42.939s user 1m41.591s sys 0m0.296s
----- gen.c #define M 2000 #define N 400 int main(void){ int i, j; srand(time()); for (i=0; i<M; i++){ for (j=0; j<N; j++){ printf("%d", rand() % 100 ); if (j < N - 1) printf(","); else printf("\n"); } } return 0; }
----- hoge.rb result = [] ARGF.each do |line| lineno = ARGF.file.lineno - 1 result[lineno] ||= [] line.chomp.split(',').collect{|i|i.to_i}.each_with_index do |val, i| begin result[lineno][i] += val rescue NoMethodError result[lineno][i] = val end end end result.each{|i|puts i.join(',')}
initial_endidx=10 arr=("01".."#{initial_endidx}").to_a num=11 while chunk = gets("") arr.push num.to_s i = arr.shift File.open("xxx-#{i}.xls", "w") do |f| f.puts chunk num = num +1 end end
#!/usr/bin/env ruby -Ke def gengo2seireki(str) h = {"m"=>1867, "t"=>1911, "s"=>1925, "h"=>1988, "明治"=>1867, "大正"=>1911, "昭和"=>1925, "平成"=>1988} p h.keys.join('|') if /^(#{h.keys.join('|')}])(\d{1,2})/i =~ str h[Regexp.last_match[1].downcase] + Regexp.last_match[2].to_i end end p gengo2seireki("昭和20") => 1945