summaryrefslogtreecommitdiff
path: root/kakeibo-monthly.pl
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2022-10-20 11:03:01 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2022-10-20 11:03:01 +0900
commit971bbb997d3a69de55831b52859962d5911511c7 (patch)
treeb0f0889cfdd225f8f78cd8432e112aa8728aa1ba /kakeibo-monthly.pl
Initial commit.HEADmain
Diffstat (limited to 'kakeibo-monthly.pl')
-rw-r--r--kakeibo-monthly.pl33
1 files changed, 33 insertions, 0 deletions
diff --git a/kakeibo-monthly.pl b/kakeibo-monthly.pl
new file mode 100644
index 0000000..160351a
--- /dev/null
+++ b/kakeibo-monthly.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+use List::Util qw(sum);
+
+my (%cost, %month);
+while (<>) {
+ chomp;
+ s/^(\d{4}-\d{2})-\d{2}/$1/; # YYYY-MM-DD -> YYYY-MM
+ my ($month, $category, undef, $cost) = split /\t/;
+ $month{$month} = 1;
+ $cost{$category} = {} unless $cost{$category};
+ &check_consistency($cost{$category}->{$month}, $cost, $month, $category);
+ $cost{$category}->{$month} += $cost;
+}
+
+my @category = sort(keys %cost);
+my @month = sort(keys %month);
+say join("\t" => "Month", @category, "Out", "In", "Total");
+for my $month (@month) {
+ my @cost = map { $cost{$_}->{$month} // 0 } @category;
+ my $out = sum(grep { $_ > 0 } @cost) // 0;
+ my $in = sum(grep { $_ < 0 } @cost) // 0;
+ my $total = sum(@cost) // 0;
+ say join("\t" => $month, @cost, $out, $in, $total);
+}
+
+# 月毎にカテゴリーのコストの正負が一貫していることをチェックする
+sub check_consistency($sum, $cost, $month, $category) {
+ !defined($sum) || $sum >= 0 == $cost >= 0
+ or die "$month の $category カテゴリーのコストの正負が一貫していません";
+}