blog/content/post/2008/10/25/2008-10-25-00001057.md

82 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: ハッシュのハッシュをつくるよ
author: kazu634
date: 2008-10-25
url: /2008/10/25/_1134/
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:4351;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
次のような形式のデータを、%hash{income}{income}のようにしてアクセスできるようにするためのスクリプトを書くよ。<a href="http://150.7.25.241/~ee22389j/html/perl/hash.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://150.7.25.241/~ee22389j/html/perl/hash.html', 'ハッシュのハッシュ');" target="_blank">ハッシュのハッシュ</a>が参考になりました。
</p>
<pre class="syntax-highlight">
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>Data::Dumper;
<span class="synComment"># === Variables ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$category</span>;
<span class="synStatement">my</span> <span class="synIdentifier">%hash</span>;
<span class="synComment"># === Main part ===</span>
<span class="synStatement">while</span> (<span class="synIdentifier">&#60;DATA&#62;</span>) {
<span class="synStatement">chomp</span>;
<span class="synStatement">if</span> (<span class="synStatement">/</span><span class="synConstant">^</span><span class="synSpecial">\[(.*)\]</span><span class="synConstant">$</span><span class="synStatement">/</span>) {
<span class="synIdentifier">$category</span> = <span class="synIdentifier">$1</span>;
} <span class="synStatement">elsif</span> (<span class="synStatement">/</span><span class="synConstant">^</span><span class="synSpecial">(.*)</span><span class="synConstant">=</span><span class="synSpecial">(.*)</span><span class="synConstant">$</span><span class="synStatement">/</span>) {
<span class="synIdentifier">$hash</span>{<span class="synIdentifier">$category</span>}{<span class="synIdentifier">$1</span>} = <span class="synIdentifier">$2</span>;
}
}
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$x</span> (<span class="synStatement">keys</span> <span class="synIdentifier">%hash</span>) {
<span class="synStatement">print</span> <span class="synIdentifier">$x</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$y</span> (<span class="synStatement">keys</span> %{<span class="synIdentifier">$hash</span>{<span class="synIdentifier">$x</span>}}) {
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$y</span><span class="synConstant">: </span><span class="synIdentifier">$hash</span><span class="synConstant">{</span><span class="synIdentifier">$x</span><span class="synConstant">}{</span><span class="synIdentifier">$y</span><span class="synConstant">}</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
}
<span class="synComment">__DATA__</span>
<span class="synComment">[income]</span>
<span class="synComment">income=174621</span>
<span class="synComment">extra=11000</span>
<span class="synComment">extra=7500</span>
<span class="synComment">[fixed charge]</span>
<span class="synComment">phone=5318</span>
<span class="synComment">meals=11834</span>
<span class="synComment">scholarship=9000</span>
<span class="synComment">[daily pay]</span>
<span class="synComment">20081001=12640 | フェルメール展</span>
<span class="synComment">20081002=51500 | Ayur-chair</span>
<span class="synComment">20081003=750</span>
<span class="synComment">20081004=4200</span>
<span class="synComment">20081005=5500</span>
<span class="synComment">20081006=11200 | 交通費: 10000→次月に支給…のはず</span>
<span class="synComment">20081007=1300</span>
<span class="synComment">20081008=6600 | 立川で泊まった</span>
<span class="synComment">20081009=1360</span>
<span class="synComment">20081010=1500</span>
<span class="synComment">20081011=4400</span>
<span class="synComment">20081012=3520 | クリーニング</span>
<span class="synComment">20081013=6900 | 床屋さん</span>
<span class="synComment">20081014=1520</span>
<span class="synComment">20081015=980</span>
<span class="synComment">20081016=1410</span>
<span class="synComment">20081017=5920 | ミュージカル・「Family」</span>
<span class="synComment">20081018=1930 | SW用写真</span>
<span class="synComment">20081019=4420 | クリーニング</span>
<span class="synComment">20081020=960</span>
<span class="synComment">20081021=1750</span>
<span class="synComment">20081022=2000</span>
<span class="synComment">20081023=5000 | 交通費</span>
<span class="synComment">20081024=1030</span>
<span class="synComment">20081025=</span>
<span class="synComment">20081026=</span>
<span class="synComment">20081027=</span>
<span class="synComment">20081028=</span>
<span class="synComment">20081029=</span>
<span class="synComment">20081030=</span>
<span class="synComment">20081031=</span>
</pre>
</div>