blog/content/post/2008/09/27/2008-09-27-00001039.md

12 KiB

title author date url wordtwit_post_info categories
最近のプロジェクト kazu634 2008-09-27 /2008/09/27/_1116/
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4305;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
Perl

家計簿をつけようとして、perlでHTML出力を目指す。だいたい完成しているんだけど、ファイル名とか決めうちだから不便。ちょっと改造中。

カレントディレクトリにあるcsvファイルを列挙し、選択したcsvからデータを呼び出すようにしてみた。csvファイルの構造はこんな感じ:

date, amount, etc

[20080901], 1180,

[20080902], 300,

[20080903], 440,

[20080904], 520,

[20080905], 4250, Dinner!

[20080906], 2930,

[20080907], 2520,

データを取り出すスクリプト:

# === Libraries ===
use strict;
use warnings;
# === Variables ===
my $dirname = '.';    # specify the directory.
my $line;             # Storing the data from STDIN, FILE
my $result;           # Storing the result
my @csv_files;        # Storing the csv filenames
my @data;    # Storing the data
my @date;    # Storing the dates
my @pay;     # Storing the amount of money spent
my @pay_etc
; # Storing the amount of money spent (unconsciously drawn from the bank account)
my $income;    # Storing the amount of income
my $i = ;            # temporary variable
my @temp;             # temporary array
# === Main part ===
$result = &push_print_choose();
&read_csv($csv_files[$result]);
my $sum_cnstnt = ;
my $sum_etc = ;
my $sum = ;
foreach my $x (@pay) {
$sum_cnstnt += $x;
}
foreach my $x (@pay_etc) {
$sum_etc += $x;
}
$sum = $sum_cnstnt + $sum_etc;
print("Pay: $sum_cnstnt\n");
print("Pay_etc: $sum_etc\n");
print("Sum of the money spent: $sum\n");
exit;
# ===================
# === sub routines===
# ===================
sub push_print_choose {
# Open the directory
opendir( DIR, $dirname ) or die "$dirname: $!";
# Get the csv files. Maybe ridiculous to make sure that
# this script work in 3000 b.c or so.
while ( my $dir = readdir(DIR) ) {
push( @csv_files, $dir ) if ( $dir =~ /\d\d\d\d\d\d\.csv$/ );
}
# close the directory
closedir(DIR);
# show the csv files
print("Here are the lists of the csv files:\n");
# $i is the number of the files in @csv_files
foreach my $x (@csv_files) {
$i++;
print("$i: $x\n");
}
# choose only one file
do {
print("Please enter the number of the csvfile you want to read: ");
$line = <STDIN>;
chomp($line);
} until (($line =~ /\d+/) && ( <= $line) && ($line <= $i));
# return the index of @csv_files
return ($line - 1);
}
sub read_csv {
my $fname = shift();
print($fname, "\n");
open(FILE, $fname) or die "$!";
$line=<FILE>;
while ($line = <FILE>) {
@temp = split(/,/, $line);
# Change the format of the date
$temp[] =~ s/^\[//;
$temp[] =~ s/\]$//;
$temp[] =~ s/^\d\d\d\d\d\d//;
# Omitting the meaningless spaces
$temp[1] =~ s/\s//g;
$temp[2] =~ s/\s//g;
# when $temp[1] eq "", change its value into 0.
if ( $temp[1] eq "" ) {
$temp[1] = ;
}
if ( $temp[] eq "etc" ) {
push( @pay_etc, $temp[1] );
} elsif ($temp[] eq "income") {
$income = $temp[];
} else {
push(@date, $temp[]);
push(@pay, $temp[1]);
}
}
close(FILE);
# Squeeze the data into @data
push( @data, [@date] );
push( @data, [@pay] );
}