93 lines
4.2 KiB
Markdown
93 lines
4.2 KiB
Markdown
---
|
|
title: 『新版Perl言語プログラミングレッスン入門編』 第11章 ファイル操作のつづき
|
|
author: kazu634
|
|
date: 2008-03-16
|
|
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:3829;}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>
|
|
練習問題
|
|
</h4>
|
|
|
|
<p>
|
|
次のようなファイルを読み込んで、数字部分を足してその合計順に表示させる。
|
|
</p>
|
|
|
|
<blockquote>
|
|
<p>
|
|
さとうはなこ,佐藤花子,65,90,100,80,73
|
|
</p>
|
|
|
|
<p>
|
|
あべかずま,阿部一馬,8,7,23,21,24
|
|
</p>
|
|
|
|
<p>
|
|
いとうこういち,伊藤浩一,74,31,41,59,38
|
|
</p>
|
|
|
|
<p>
|
|
さとうたろう,佐藤太郎,100,95,98,82,61
|
|
</p>
|
|
|
|
<p>
|
|
むらまつしんじ,村松信二,55,48,79,90,88
|
|
</p>
|
|
|
|
<p>
|
|
しんどうさんたろう,新藤三太郎,74,41,59,31,38
|
|
</p>
|
|
</blockquote>
|
|
|
|
<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">$tmp_score</span>;
|
|
<span class="synStatement">my</span> <span class="synIdentifier">%list</span>;
|
|
<span class="synStatement">my</span> <span class="synIdentifier">$tmp_name</span>;
|
|
<span class="synStatement">my</span> <span class="synIdentifier">@tmp</span>;
|
|
<span class="synStatement">open</span>(<span class="synIdentifier">FILE</span>, <span class="synConstant">'data.txt'</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">"</span><span class="synIdentifier">$!</span><span class="synConstant">"</span>;
|
|
<span class="synStatement">while</span> (<span class="synStatement">my</span> <span class="synIdentifier">$line</span> =<span class="synIdentifier"><FILE></span>) {
|
|
<span class="synIdentifier">@tmp</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="synIdentifier">$tmp_name</span> = <span class="synIdentifier">$tmp</span>[<span class="synConstant">1</span>];
|
|
<span class="synIdentifier">$tmp_score</span> = <span class="synIdentifier">$tmp</span>[<span class="synConstant">2</span>] + <span class="synIdentifier">$tmp</span>[<span class="synConstant">3</span>] + <span class="synIdentifier">$tmp</span>[<span class="synConstant">4</span>] + <span class="synIdentifier">$tmp</span> [<span class="synConstant">5</span>] + <span class="synIdentifier">$tmp</span>[<span class="synConstant">6</span>];
|
|
<span class="synIdentifier">$list</span>{<span class="synIdentifier">$tmp_name</span>} = <span class="synIdentifier">$tmp_score</span>;
|
|
}
|
|
<span class="synStatement">close</span>(<span class="synIdentifier">FILE</span>);
|
|
<span class="synStatement">foreach</span> (<span class="synStatement">reverse</span>(<span class="synStatement">sort</span> {<span class="synIdentifier">$list</span>{<span class="synIdentifier">$a</span>} <=> <span class="synIdentifier">$list</span>{<span class="synIdentifier">$b</span>} } <span class="synStatement">keys</span> <span class="synIdentifier">%list</span>)) {
|
|
<span class="synStatement">print</span> <span class="synConstant">"</span><span class="synIdentifier">$_</span><span class="synConstant"> : </span><span class="synIdentifier">$list</span><span class="synConstant">{</span><span class="synIdentifier">$_</span><span class="synConstant">}</span><span class="synSpecial">\n</span><span class="synConstant">"</span>;
|
|
}
|
|
</pre>
|
|
|
|
<p>
|
|
回答を見て思ったこと。
|
|
</p>
|
|
|
|
<ul>
|
|
<li>
|
|
my ($yomi, $name, @socres) = split (/,/, $line); という書き方はありなんですね。。。
|
|
</li>
|
|
</ul>
|
|
|
|
<p>
|
|
あと、ハッシュの使い方とかソートの仕方とかを忘れていた(..;)
|
|
</p>
|
|
</div>
|