blog/content/post/2009/03/08/2009-03-08-00001124.md

174 lines
9.2 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: ハッシュの扱い方について
author: kazu634
date: 2009-03-08
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:4517;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
Perlでスクリプトを書いていてはまったので、とりあえず備忘録としてまとめておきます。
</p>
<h4>
ハッシュリファレンス
</h4>
<pre class="syntax-highlight">
<span class="synStatement">my</span> <span class="synIdentifier">%month</span> = (
<span class="synConstant">January </span>=&#62; <span class="synConstant">1</span>,
<span class="synConstant">Feburary </span>=&#62; <span class="synConstant">2</span>,
<span class="synConstant">March </span>=&#62; <span class="synConstant">3</span>,
<span class="synConstant">April </span>=&#62; <span class="synConstant">4</span>,
<span class="synConstant">May </span>=&#62; <span class="synConstant">5</span>,
<span class="synConstant">June </span>=&#62; <span class="synConstant">6</span>,
<span class="synConstant">July </span>=&#62; <span class="synConstant">7</span>,
<span class="synConstant">August </span>=&#62; <span class="synConstant">8</span>,
<span class="synConstant">September </span>=&#62; <span class="synConstant">9</span>,
<span class="synConstant">October </span>=&#62; <span class="synConstant">10</span>,
<span class="synConstant">November </span>=&#62; <span class="synConstant">11</span>,
<span class="synConstant">December </span>=&#62; <span class="synConstant">12</span>,
);
<span class="synComment"># ハッシュリファレンスを$ref_hashに格納</span>
<span class="synStatement">my</span> <span class="synIdentifier">$ref_hash</span> = <span class="synIdentifier">\%month</span>;
<span class="synStatement">print</span>(<span class="synConstant">&#34;reference to month: </span><span class="synIdentifier">$ref_hash</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synComment"># デリファレンスするには%$ref_hashのように表記する</span>
<span class="synStatement">my</span> <span class="synIdentifier">@sorted_key</span> = <span class="synStatement">sort</span> <span class="synStatement">keys</span> <span class="synIdentifier">%$ref_hash</span>;
<span class="synStatement">print</span>(<span class="synConstant">&#34;Sorted key: </span><span class="synIdentifier">@sorted_key</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
</pre>
<p>
ハッシュリファレンスからのハッシュの要素にアクセスするには次のような方法がある:
</p>
<pre class="syntax-highlight">
- <span class="synIdentifier">$$ref_hash</span>{January}
- <span class="synIdentifier">$ref_hash</span>-&#62;{January}
</pre>
<h4>
無名ハッシュ
</h4>
<p>
無名ハッシュは「{}」で指定してあげる:
</p>
<pre class="syntax-highlight">
<span class="synStatement">my</span> <span class="synIdentifier">$ref_hash</span> = {
<span class="synConstant">January </span>=&#62; <span class="synConstant">1</span>,
<span class="synConstant">Feburary </span>=&#62; <span class="synConstant">2</span>,
<span class="synConstant">March </span>=&#62; <span class="synConstant">3</span>,
<span class="synConstant">April </span>=&#62; <span class="synConstant">4</span>,
<span class="synConstant">May </span>=&#62; <span class="synConstant">5</span>,
<span class="synConstant">June </span>=&#62; <span class="synConstant">6</span>,
<span class="synConstant">July </span>=&#62; <span class="synConstant">7</span>,
<span class="synConstant">August </span>=&#62; <span class="synConstant">8</span>,
<span class="synConstant">September </span>=&#62; <span class="synConstant">9</span>,
<span class="synConstant">October </span>=&#62; <span class="synConstant">10</span>,
<span class="synConstant">November </span>=&#62; <span class="synConstant">11</span>,
<span class="synConstant">December </span>=&#62; <span class="synConstant">12</span>,
};
</pre>
<h4>
2次元ハッシュ
</h4>
<p>
こんな感じで書くといい:
</p>
<pre class="syntax-highlight">
<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">$tmp_key</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">$data</span>{<span class="synIdentifier">$tmp_key</span>}{<span class="synIdentifier">$1</span>} = <span class="synIdentifier">$2</span>;
}
}
<span class="synComment"># print Dumper(%data);</span>
}
<span class="synComment">__DATA__</span>
<span class="synComment">[total_income]</span>
<span class="synComment">income: 227706</span>
<span class="synComment">extra_income: 15000</span>
<span class="synComment">[fixed_pay]</span>
<span class="synComment">cell_phone1: 2213</span>
<span class="synComment">cell_phone2: 9750</span>
<span class="synComment">meals: 13230</span>
<span class="synComment">scholarship: 9000</span>
<span class="synComment">provider: 4095</span>
<span class="synComment">[commute]</span>
<span class="synComment">commute: 39890</span>
<span class="synComment">[daily_pay]</span>
<span class="synComment">2/01: 7130</span>
<span class="synComment">2/02: 1044</span>
<span class="synComment">2/03: 1710</span>
<span class="synComment">2/04: 9360</span>
<span class="synComment">2/05: 6810</span>
<span class="synComment">2/06: 1405</span>
<span class="synComment">2/07: 7608</span>
<span class="synComment">2/08: 670</span>
<span class="synComment">2/09: 1374</span>
<span class="synComment">2/10: 3059</span>
<span class="synComment">2/11: 5205</span>
<span class="synComment">2/12: 1470</span>
<span class="synComment">2/13: 750</span>
<span class="synComment">2/14: 407</span>
<span class="synComment">2/15: 2774</span>
<span class="synComment">2/16: 2510</span>
<span class="synComment">2/17: 1320</span>
<span class="synComment">2/18: 1740</span>
<span class="synComment">2/19: 6910</span>
<span class="synComment">2/20: 2837</span>
<span class="synComment">2/21: 7620</span>
<span class="synComment">2/22: 6277</span>
<span class="synComment">2/23: 1730</span>
<span class="synComment">2/24: 2624</span>
<span class="synComment">2/25: 4347</span>
<span class="synComment">2/26: 1299</span>
<span class="synComment">2/27: 987</span>
<span class="synComment">2/28: 4900</span>
</pre>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4774135046/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774135046/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/410B5%2BzbIxL._SL160_.jpg" class="hatena-asin-detail-image" alt="すぐわかる オブジェクト指向 Perl" title="すぐわかる オブジェクト指向 Perl" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4774135046/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774135046/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'すぐわかる オブジェクト指向 Perl');">すぐわかる オブジェクト指向 Perl</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%BF%BC%C2%F4%C0%E9%BF%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%BF%BC%C2%F4%C0%E9%BF%D2', '深沢千尋');" class="keyword">深沢千尋</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2', '技術評論社');" class="keyword">技術評論社</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 2008/06/20
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 大型本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 12人 <span class="hatena-asin-detail-label">クリック</span>: 195回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4774135046" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774135046', 'この商品を含むブログ (35件) を見る');" target="_blank">この商品を含むブログ (35件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>