Prev / Next / あかぴのChangeLog by rchalow

cl2moneycsv.rb[chalow][修行]

2006-11-30

cl2moneycsv.rb [chalow][修行]

ありがとさんです>kou

#!/usr/bin/env ruby
$KCODE="EUC"

def usage
  STDERR.print <<EOF
usage: #{$0} [-m] [files...]
  m: monthly
EOF
  exit 1
end

Opt = {
  'monthly' => false    # 月毎に計算
}      # Optはハッシュ

while ARGV[0] =~ /^-/
  $_ = ARGV.shift
  Opt['monthly'] = true if ~/m/
  usage if ~/[^-m]/
end

usage if ARGV.size < 1


# 費目 an item of expendidure
#lioe = ['外', '食', '交', '遊', '本', '音', '雑', '衣', '他']  # 配列
lioe = []

date =""
inside_flag = 0
entry = Hash.new
while line = gets
  if /^((\d{4}-\d\d)-\d\d)/ =~ line then  # 日付をキープ
    if Opt['monthly'] == true then
    date = $2      # = year-month
    else
    date = $1
    date = date.tr("-",".")
    end
  elsif /買物ログ.*:/ =~ line then  # 家計簿データ記述ブロックの始まり
    inside_flag = 1
  elsif inside_flag == 1
    if /^\s*$/ =~ line and inside_flag == 1 then
      inside_flag = 0
    elsif (/^\t(.+?)(\s|\xa1\xa1).*(\s|\xa1\xa1)(\d+)$/) =~ line then
yo = line.split(nil)
  kamoku = yo[0]
  naiyou = yo[1]
  kingaku = yo[2]
  sonota = yo[3]

          lioe.push(yo[0])
          lioe.uniq!

      entry[date] ||= Hash.new(0)
      entry[date][$1] += $4.to_i
    end
  end
end

print " " * 10, ",  ", lioe.join(',  '), "\n"

entry.keys.sort.each do |date|
  print "#{date},"
  puts lioe.collect {|item| "%4d" % entry[date][item]}.join(",")
end

あっしは lioe を任意にしたいので
#!/usr/bin/env ruby
$KCODE="EUC"

def usage
  STDERR.print <<EOF
usage: #{$0} [-m] [files...]
  m: monthly
EOF
  exit 1
end

Opt = {
  'monthly' => false    # 月毎に計算
}      # Optはハッシュ

while ARGV[0] =~ /^-/
  $_ = ARGV.shift
  Opt['monthly'] = true if ~/m/
  usage if ~/[^-m]/
end

usage if ARGV.size < 1


# 費目 an item of expendidure
#lioe = ['外', '食', '交', '遊', '本', '音', '雑', '衣', '他']  # 配列
lioe = []

date =""
inside_flag = 0
entry = Hash.new
while line = gets
  if /^((\d{4}-\d\d)-\d\d)/ =~ line then  # 日付をキープ
    if Opt['monthly'] == true then
    date = $2      # = year-month
    else
    date = $1
    date = date.tr("-",".")
    end
  elsif /買物ログ.*:/ =~ line then  # 家計簿データ記述ブロックの始まり
    inside_flag = 1
  elsif inside_flag == 1
    if /^\s*$/ =~ line and inside_flag == 1 then
      inside_flag = 0
    elsif (/^\t(.+?)(\s|\xa1\xa1).*(\s|\xa1\xa1)(\d+)$/) =~ line then
yo = line.split(nil)
  kamoku = yo[0]
  naiyou = yo[1]
  kingaku = yo[2]
  sonota = yo[3]

          lioe.push(yo[0])
          lioe.uniq!

      entry[date] ||= Hash.new(0)
      entry[date][$1] += $4.to_i
    end
  end
end

print " " * 10, ",  ", lioe.join(',  '), "\n"

entry.keys.sort.each do |date|
  print "#{date},"
  puts lioe.collect {|item| "%4d" % entry[date][item]}.join(",")
end

blog comments powered by Disqus