blog/content/post/2008/02/14/2008-02-14-00000800.md

145 lines
8.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 『新版Perl言語プログラミングレッスン入門編』第六章・繰り返し つづき
author: kazu634
date: 2008-02-14
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:3731;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
- Programming
---
<div class="section">
<p>
 つづきだよー
</p>
<p>
<a name="seemore"></a>
</p>
<h4>
mapによる繰り返し
</h4>
<p>
 関数mapは次のような形式で使うよ。
</p>
<pre class="syntax-highlight">
<span class="synStatement">map</span> { 各要素を構成する式 } リスト;
</pre>
<p>
例えばこいつはリストの各要素を二乗したものを返すことになる:
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">my</span> <span class="synIdentifier">@square</span> = <span class="synStatement">map</span>{ <span class="synIdentifier">$_</span> **<span class="synConstant">2</span> } (<span class="synConstant"></span> .. <span class="synConstant">9</span>);
<span class="synStatement">foreach</span> (<span class="synIdentifier">@square</span>) {
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$_</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
数字計算だけじゃなくても大丈夫だよ。これもOKだよ。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># ユーザー名</span>
<span class="synStatement">my</span> <span class="synIdentifier">@user</span> =(<span class="synConstant">'kazu'</span>, <span class="synConstant">'take'</span>, <span class="synConstant">'yukari'</span>, <span class="synConstant">'nanbu'</span>);
<span class="synComment"># メールアドレスの作成</span>
<span class="synStatement">my</span> <span class="synIdentifier">@adr</span> = <span class="synStatement">map</span> { <span class="synIdentifier">$_</span> . <span class="synConstant">'@gmail.com'</span>} <span class="synIdentifier">@user</span>;
<span class="synComment"># 表示</span>
<span class="synStatement">print</span> <span class="synStatement">join</span>(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, <span class="synIdentifier">@adr</span>);
</pre>
<h4>
パターンマッチの/g修飾子による繰り返し
</h4>
<p>
 パターンマッチの/g修飾子は行全体に渡ってパターンマッチを行う指示ですので、繰り返しの一種と見なすこともできます。例えば
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># 適当な文字列を作るよ</span>
<span class="synStatement">my</span> <span class="synIdentifier">$line</span> = <span class="synConstant">'168cm, 58kg, 37 years old'</span>;
<span class="synComment"># 繰り返しがあるかを見るよ</span>
<span class="synStatement">my</span> <span class="synIdentifier">@match</span> = (<span class="synIdentifier">$line</span> =~<span class="synStatement"> /</span><span class="synSpecial">[0-9]+</span><span class="synStatement">/g</span>);
<span class="synComment"># 表示</span>
<span class="synStatement">print</span> <span class="synStatement">join</span>(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, <span class="synIdentifier">@match</span>), <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
</pre>
<p>
は変数$lineの中に含まれているすべての文字列を抽出して配列@matchに格納します。
</p>
<h4>
練習問題
</h4>
<p>
問:カレントディレクトリのファイル一覧をソートして表示するプログラム。ただしカレントディレクトリと親ディレクトリは表示させないようにする。
</p>
<p>
私の回答:
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># カレントディレクトリを開くよ</span>
<span class="synStatement">opendir</span> (<span class="synIdentifier">DIR</span>, <span class="synConstant">'.'</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="synComment"># カレントディレクトリのファイル一覧を取得して、配列@flistに格納</span>
<span class="synStatement">my</span> <span class="synIdentifier">@flist</span> = <span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>);
<span class="synComment"># 先頭に「.」があるやつを除外すれば大丈夫だろう</span>
<span class="synIdentifier">@flist</span> = <span class="synStatement">grep</span> (<span class="synStatement">/</span><span class="synConstant">^[^</span><span class="synSpecial">\.</span><span class="synConstant">]</span><span class="synStatement">/</span>, <span class="synIdentifier">@flist</span>);
<span class="synStatement">foreach</span> (<span class="synStatement">sort</span>(<span class="synIdentifier">@flist</span>)) {
<span class="synStatement">print</span> <span class="synStatement">join</span>(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, <span class="synIdentifier">$_</span>);
}
<span class="synStatement">closedir</span> (<span class="synIdentifier">DIR</span>);
</pre>
<p>
問: 16進数の表を作ってください。
</p>
<p>
私の回答:
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># 16進数の1桁が取りうる範囲</span>
<span class="synStatement">my</span> <span class="synIdentifier">@range</span> = (<span class="synConstant">'0'</span>, <span class="synConstant">'1'</span>, <span class="synConstant">'2'</span>, <span class="synConstant">'3'</span>, <span class="synConstant">'4'</span>, <span class="synConstant">'5'</span>, <span class="synConstant">'6'</span>, <span class="synConstant">'7'</span>, <span class="synConstant">'8'</span>, <span class="synConstant">'9'</span>, <span class="synConstant">'A'</span>, <span class="synConstant">'B'</span>, <span class="synConstant">'C'</span>, <span class="synConstant">'D'</span>, <span class="synConstant">'E'</span>, <span class="synConstant">'F'</span>);
<span class="synComment"># 16進数表の作成</span>
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$f</span> (<span class="synIdentifier">@range</span>) {
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$s</span> (<span class="synIdentifier">@range</span>) {
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$f$s</span><span class="synConstant"> &#34;</span>;
}
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
こう書いても大丈夫らしい。
</p>
<pre class="syntax-highlight">
<span class="synStatement">my</span> <span class="synIdentifier">@hex</span> = (<span class="synConstant">'0'</span> .. <span class="synConstant">'9'</span>, <span class="synConstant">'A'</span> .. <span class="synConstant">'F'</span>);
</pre>
<p>
なんか自由だ。
</p>
</div>