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

155 lines
12 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 最近のプロジェクト
author: kazu634
date: 2008-09-27
wordtwit_post_info:
- '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";}}'
categories:
- Perl
---
<div class="section">
<p>
家計簿をつけようとして、perlでHTML出力を目指す。だいたい完成しているんだけど、ファイル名とか決めうちだから不便。ちょっと改造中。
</p>
<p>
カレントディレクトリにあるcsvファイルを列挙し、選択したcsvからデータを呼び出すようにしてみた。csvファイルの構造はこんな感じ:
</p>
<blockquote>
<p>
date, amount, etc
</p>
<p>
[20080901], 1180,
</p>
<p>
[20080902], 300,
</p>
<p>
[20080903], 440,
</p>
<p>
[20080904], 520,
</p>
<p>
[20080905], 4250, Dinner!
</p>
<p>
[20080906], 2930,
</p>
<p>
[20080907], 2520,
</p>
</blockquote>
<p>
データを取り出すスクリプト:
</p>
<pre class="syntax-highlight">
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># === Variables ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$dirname</span> = <span class="synConstant">'.'</span>; <span class="synComment"># specify the directory.</span>
<span class="synStatement">my</span> <span class="synIdentifier">$line</span>; <span class="synComment"># Storing the data from STDIN, FILE</span>
<span class="synStatement">my</span> <span class="synIdentifier">$result</span>; <span class="synComment"># Storing the result</span>
<span class="synStatement">my</span> <span class="synIdentifier">@csv_files</span>; <span class="synComment"># Storing the csv filenames</span>
<span class="synStatement">my</span> <span class="synIdentifier">@data</span>; <span class="synComment"># Storing the data</span>
<span class="synStatement">my</span> <span class="synIdentifier">@date</span>; <span class="synComment"># Storing the dates</span>
<span class="synStatement">my</span> <span class="synIdentifier">@pay</span>; <span class="synComment"># Storing the amount of money spent</span>
<span class="synStatement">my</span> <span class="synIdentifier">@pay_etc</span>
; <span class="synComment"># Storing the amount of money spent (unconsciously drawn from the bank account)</span>
<span class="synStatement">my</span> <span class="synIdentifier">$income</span>; <span class="synComment"># Storing the amount of income</span>
<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synComment"># temporary variable</span>
<span class="synStatement">my</span> <span class="synIdentifier">@temp</span>; <span class="synComment"># temporary array</span>
<span class="synComment"># === Main part ===</span>
<span class="synIdentifier">$result</span> = <span class="synIdentifier">&#38;push_print_choose</span>();
<span class="synIdentifier">&#38;read_csv</span>(<span class="synIdentifier">$csv_files</span>[<span class="synIdentifier">$result</span>]);
<span class="synStatement">my</span> <span class="synIdentifier">$sum_cnstnt</span> = <span class="synConstant"></span>;
<span class="synStatement">my</span> <span class="synIdentifier">$sum_etc</span> = <span class="synConstant"></span>;
<span class="synStatement">my</span> <span class="synIdentifier">$sum</span> = <span class="synConstant"></span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$x</span> (<span class="synIdentifier">@pay</span>) {
<span class="synIdentifier">$sum_cnstnt</span> += <span class="synIdentifier">$x</span>;
}
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$x</span> (<span class="synIdentifier">@pay_etc</span>) {
<span class="synIdentifier">$sum_etc</span> += <span class="synIdentifier">$x</span>;
}
<span class="synIdentifier">$sum</span> = <span class="synIdentifier">$sum_cnstnt</span> + <span class="synIdentifier">$sum_etc</span>;
<span class="synStatement">print</span>(<span class="synConstant">&#34;Pay: </span><span class="synIdentifier">$sum_cnstnt</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">print</span>(<span class="synConstant">&#34;Pay_etc: </span><span class="synIdentifier">$sum_etc</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">print</span>(<span class="synConstant">&#34;Sum of the money spent: </span><span class="synIdentifier">$sum</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">exit</span>;
<span class="synComment"># ===================</span>
<span class="synComment"># === sub routines===</span>
<span class="synComment"># ===================</span>
<span class="synStatement">sub</span><span class="synIdentifier"> push_print_choose </span>{
<span class="synComment"># Open the directory</span>
<span class="synStatement">opendir</span>( <span class="synIdentifier">DIR</span>, <span class="synIdentifier">$dirname</span> ) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname</span><span class="synConstant">: </span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synComment"># Get the csv files. Maybe ridiculous to make sure that</span>
<span class="synComment"># this script work in 3000 b.c or so.</span>
<span class="synStatement">while</span> ( <span class="synStatement">my</span> <span class="synIdentifier">$dir</span> = <span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>) ) {
<span class="synStatement">push</span>( <span class="synIdentifier">@csv_files</span>, <span class="synIdentifier">$dir</span> ) <span class="synStatement">if</span> ( <span class="synIdentifier">$dir</span> =~<span class="synStatement"> /</span><span class="synSpecial">\d\d\d\d\d\d\.</span><span class="synConstant">csv$</span><span class="synStatement">/</span> );
}
<span class="synComment"># close the directory</span>
<span class="synStatement">closedir</span>(<span class="synIdentifier">DIR</span>);
<span class="synComment"># show the csv files</span>
<span class="synStatement">print</span>(<span class="synConstant">&#34;Here are the lists of the csv files:</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synComment"># $i is the number of the files in @csv_files</span>
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$x</span> (<span class="synIdentifier">@csv_files</span>) {
<span class="synIdentifier">$i</span>++;
<span class="synStatement">print</span>(<span class="synConstant">&#34;</span><span class="synIdentifier">$i</span><span class="synConstant">: </span><span class="synIdentifier">$x</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synComment"># choose only one file</span>
<span class="synStatement">do</span> {
<span class="synStatement">print</span>(<span class="synConstant">&#34;Please enter the number of the csvfile you want to read: &#34;</span>);
<span class="synIdentifier">$line</span> = <span class="synIdentifier">&#60;STDIN&#62;</span>;
<span class="synStatement">chomp</span>(<span class="synIdentifier">$line</span>);
} <span class="synStatement">until</span> ((<span class="synIdentifier">$line</span> =~<span class="synStatement"> /</span><span class="synSpecial">\d+</span><span class="synStatement">/</span>) &#38;&#38; (<span class="synConstant"></span> &#60;= <span class="synIdentifier">$line</span>) &#38;&#38; (<span class="synIdentifier">$line</span> &#60;= <span class="synIdentifier">$i</span>));
<span class="synComment"># return the index of @csv_files</span>
<span class="synStatement">return</span> (<span class="synIdentifier">$line</span> - <span class="synConstant">1</span>);
}
<span class="synStatement">sub</span><span class="synIdentifier"> read_csv </span>{
<span class="synStatement">my</span> <span class="synIdentifier">$fname</span> = <span class="synStatement">shift</span>();
<span class="synStatement">print</span>(<span class="synIdentifier">$fname</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">open</span>(<span class="synIdentifier">FILE</span>, <span class="synIdentifier">$fname</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synIdentifier">$line</span>=<span class="synIdentifier">&#60;FILE&#62;</span>;
<span class="synStatement">while</span> (<span class="synIdentifier">$line</span> = <span class="synIdentifier">&#60;FILE&#62;</span>) {
<span class="synIdentifier">@temp</span> = <span class="synStatement">split</span>(<span class="synStatement">/</span><span class="synConstant">,</span><span class="synStatement">/</span>, <span class="synIdentifier">$line</span>);
<span class="synComment"># Change the format of the date</span>
<span class="synIdentifier">$temp</span>[<span class="synConstant"></span>] =~ <span class="synStatement">s/</span><span class="synConstant">^</span><span class="synSpecial">\[</span><span class="synStatement">//</span>;
<span class="synIdentifier">$temp</span>[<span class="synConstant"></span>] =~ <span class="synStatement">s/</span><span class="synSpecial">\]</span><span class="synConstant">$</span><span class="synStatement">//</span>;
<span class="synIdentifier">$temp</span>[<span class="synConstant"></span>] =~ <span class="synStatement">s/</span><span class="synConstant">^</span><span class="synSpecial">\d\d\d\d\d\d</span><span class="synStatement">//</span>;
<span class="synComment"># Omitting the meaningless spaces</span>
<span class="synIdentifier">$temp</span>[<span class="synConstant">1</span>] =~ <span class="synStatement">s/</span><span class="synSpecial">\s</span><span class="synStatement">//g</span>;
<span class="synIdentifier">$temp</span>[<span class="synConstant">2</span>] =~ <span class="synStatement">s/</span><span class="synSpecial">\s</span><span class="synStatement">//g</span>;
<span class="synComment"># when $temp[1] eq &#34;&#34;, change its value into 0.</span>
<span class="synStatement">if</span> ( <span class="synIdentifier">$temp</span>[<span class="synConstant">1</span>] <span class="synStatement">eq</span> <span class="synConstant">&#34;&#34;</span> ) {
<span class="synIdentifier">$temp</span>[<span class="synConstant">1</span>] = <span class="synConstant"></span>;
}
<span class="synStatement">if</span> ( <span class="synIdentifier">$temp</span>[<span class="synConstant"></span>] <span class="synStatement">eq</span> <span class="synConstant">&#34;etc&#34;</span> ) {
<span class="synStatement">push</span>( <span class="synIdentifier">@pay_etc</span>, <span class="synIdentifier">$temp</span>[<span class="synConstant">1</span>] );
} <span class="synStatement">elsif</span> (<span class="synIdentifier">$temp</span>[<span class="synConstant"></span>] <span class="synStatement">eq</span> <span class="synConstant">&#34;income&#34;</span>) {
<span class="synIdentifier">$income</span> = <span class="synIdentifier">$temp</span>[<span class="synConstant"></span>];
} <span class="synStatement">else</span> {
<span class="synStatement">push</span>(<span class="synIdentifier">@date</span>, <span class="synIdentifier">$temp</span>[<span class="synConstant"></span>]);
<span class="synStatement">push</span>(<span class="synIdentifier">@pay</span>, <span class="synIdentifier">$temp</span>[<span class="synConstant">1</span>]);
}
}
<span class="synStatement">close</span>(<span class="synIdentifier">FILE</span>);
<span class="synComment"># Squeeze the data into @data</span>
<span class="synStatement">push</span>( <span class="synIdentifier">@data</span>, [<span class="synIdentifier">@date</span>] );
<span class="synStatement">push</span>( <span class="synIdentifier">@data</span>, [<span class="synIdentifier">@pay</span>] );
}
</pre>
</div>