Macの青色申告をHackするの続き
こないだに引き続き、今度は科目別に書き出してみました。
use v5.10;
use strict;
use warnings;
use utf8;
binmode STDOUT, ':utf8';
my $in_file = 'test.tab';
my $data_ref = load_tab_file( $in_file );
say "件数 = " . scalar(@{$data_ref});
my %hash = ();
foreach my $line (@{$data_ref}) {
my @tmp = split "\t", $line;
if ( not exists $hash{$tmp[3]} ) {
$hash{$tmp[3]} = [];
}
push @{$hash{$tmp[3]}}, \@tmp;
}
foreach my $kind (keys %hash) {
my $out_file = $kind . '.txt';
write_tab_file( $out_file, $hash{$kind} );
}
sub write_tab_file {
my $file = shift;
my $array_ref = shift;
my @data = sort {
$a->[1] cmp $b->[1];
} @{$array_ref};
say "write! " . $file;
open( my $fh, '>:encoding(shift-jis)', $file ) or die $!;
foreach (@data) {
print $fh join("\t", @{$_}) . "\r";
}
close $fh;
}
sub load_tab_file {
my $file = shift;
local $/ = "\r";
open( my $fh, '<:encoding(shift-jis)', $file ) or die $!;
my @ret = <$fh>;
chomp @ret;
close $fh;
return \@ret;
}
これを実行すると、
$ perl aaa.pl
件数 = 122
write! 一括償却資産.txt
write! 旅費交通費.txt
write! 事務用品費.txt
・
・
・
みたいな感じで、ファイルが出力されます。
これで、ちょっとは見れなくもない感じになりましたかね!
おしまい。
Leave a Comment