blog/content/post/2008/02/07/2008-02-07-『新版perl言語プログラミング...

203 lines
11 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: '『新版Perl言語プログラミングレッスン入門編』第6章: 繰り返し のつづき'
author: kazu634
date: 2008-02-07
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:3701;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
 つづきー
</p>
<p>
<a name="seemore"></a>
</p>
<h4>
while
</h4>
<p>
 繰り返しの中でlastを使うと、繰り返しを終了するよ。次のプログラムは「END」という行があったらそこで繰り返しを終了している。
</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">@array</span> = (<span class="synConstant"></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="synStatement">my</span> <span class="synIdentifier">$flag</span> = <span class="synConstant"></span>;
swhile (<span class="synIdentifier">$flag</span> &#60; <span class="synConstant">10</span>) {
<span class="synIdentifier">$flag</span> = <span class="synIdentifier">$flag</span> + <span class="synConstant">1</span>;
<span class="synStatement">if</span> (<span class="synIdentifier">$array</span>[<span class="synIdentifier">$flag</span>] == <span class="synConstant">5</span>){
<span class="synStatement">last</span>;
}
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$array</span><span class="synConstant">[</span><span class="synIdentifier">$flag</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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">@array</span> = (<span class="synConstant"></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="synStatement">my</span> <span class="synIdentifier">$flag</span> = <span class="synConstant"></span>;
<span class="synStatement">while</span> (<span class="synIdentifier">$flag</span> &#60; <span class="synConstant">10</span>) {
<span class="synIdentifier">$flag</span> = <span class="synIdentifier">$flag</span> + <span class="synConstant">1</span>;
<span class="synStatement">last</span> <span class="synStatement">if</span> (<span class="synIdentifier">$array</span>[<span class="synIdentifier">$flag</span>] == <span class="synConstant">5</span>);
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$array</span><span class="synConstant">[</span><span class="synIdentifier">$flag</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
こっちの方がスマートだ。
</p>
<p>
 繰り返しの中でnextを使うと、繰り返しを「次」に進めます。たとえば次のようなプログラムだと、配列の5番目をスキップして表示することになるよ。
</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">@array</span> = (<span class="synConstant"></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="synStatement">my</span> <span class="synIdentifier">$flag</span> = <span class="synConstant">-1</span>;
<span class="synStatement">while</span> (<span class="synIdentifier">$flag</span> &#60; <span class="synConstant">10</span>) {
<span class="synIdentifier">$flag</span> = <span class="synIdentifier">$flag</span> + <span class="synConstant">1</span>;
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$array</span>[<span class="synIdentifier">$flag</span>] == <span class="synConstant">5</span>);
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$array</span><span class="synConstant">[</span><span class="synIdentifier">$flag</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
 他にもredoとかあるよ。
</p>
<h4>
for
</h4>
<p>
 for文の構文は
</p>
<pre class="syntax-highlight">
<span class="synStatement">for</span> (初期化; 条件式; 次の一歩) {
繰り返して実行する部分
}
</pre>
<p>
となるよ。例はこんな感じ。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synIdentifier">$i</span> &#60; <span class="synConstant">10</span>; <span class="synIdentifier">$i</span>++) {
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$i</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
こいつはCライクな書き方だけど、perlではこれは次のように書くこともできる。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">for</span> (<span class="synConstant"></span> .. <span class="synConstant">9</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>
<h4>
do .. untilによる繰り返し
</h4>
<p>
 do &#8230; until構文を使うと、条件判定をはじめに行うのではなく最後に行うことができます。次の文は、標準入力から入力された文字列を表示し、endが入力されると終了します。
</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">$line</span>;
<span class="synStatement">do</span> {
<span class="synIdentifier">$line</span> = <span class="synIdentifier">&#60;STDIN&#62;</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$line</span>;
} <span class="synStatement">until</span> (<span class="synIdentifier">$line</span> <span class="synStatement">eq</span> <span class="synConstant">&#34;end</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
</pre>
<p>
do &#8230; while構文で書くこともできるけど、そのときは真偽が逆になることに注意してね。
</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">$line</span>;
<span class="synStatement">do</span> {
<span class="synIdentifier">$line</span> = <span class="synIdentifier">&#60;STDIN&#62;</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$line</span>;
} <span class="synStatement">while</span> (<span class="synIdentifier">$line</span> <span class="synStatement">ne</span> <span class="synConstant">&#34;end</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
</pre>
<h4>
文字列の繰り返し
</h4>
<p>
 演算子xを用いると、文字列の繰り返しを作ることができます。例えば次の文は&#8217;-&#8216;を40個表示します。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">print</span> <span class="synConstant">'-'</span> x <span class="synConstant">40</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
</pre>
<p>
次のようにすると、文字列$strに合わせて横線の長さが上下するよ。
</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">$str</span> = <span class="synConstant">'Lord Jim: the Failure of Education'</span>;
<span class="synStatement">print</span> <span class="synConstant">'-'</span> x <span class="synStatement">length</span>(<span class="synIdentifier">$str</span>), <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$str</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">print</span> <span class="synConstant">'-'</span> x <span class="synStatement">length</span>(<span class="synIdentifier">$str</span>), <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
</pre>
<h4>
grepによる繰り返し
</h4>
<p>
 関数grepは配列の中をすべてgrepする。
</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">@word</span> = (<span class="synConstant">'Sun'</span>, <span class="synConstant">'Mon'</span>, <span class="synConstant">'Tue'</span>, <span class="synConstant">'Wed'</span>, <span class="synConstant">'Thu'</span>, <span class="synConstant">'Fri'</span>, <span class="synConstant">'Sat'</span>);
<span class="synStatement">my</span> <span class="synIdentifier">@found</span> = <span class="synStatement">grep</span>(<span class="synStatement">/</span><span class="synConstant">^S</span><span class="synStatement">/</span>, <span class="synIdentifier">@word</span>);
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synIdentifier">@found</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
</pre>
<h4>
mapによる繰り返し
</h4>
<p>
 後で書く。
</p>
</div>