2019-03-31 11:00:21 +00:00
---
title: 『新版Perl言語プログラミングレッスン入門編』第11章 ファイル操作のつづき
author: kazu634
date: 2008-03-15
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:3827;}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 >
ファイルテスト演算子 -e
< / h4 >
< p >
あるファイルが存在するかどうかをチェックしたいときは「-e」を用いる。
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > if< / span > (< span class = "synStatement" > -e< / span > < span class = "synConstant" > 'datafile.txt'< / span > ) {
< span class = "synStatement" > print</ span > < span class = "synConstant" > 'Exists!'</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
} < span class = "synStatement" > else< / span > {
< span class = "synStatement" > print</ span > < span class = "synConstant" > 'Not Exists'</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< / pre >
< h4 >
ファイルテスト演算子 -sと-z
< / h4 >
< p >
ファイルのサイズを知りたいときは-sを用いる。「サイズが0かどうか」を知りたいときは-zを用いる。
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > print< / span > < span class = "synStatement" > -s< / span > < span class = "synConstant" > 'datafile.txt'< / span > ;
< / pre >
< h4 >
ファイルテスト演算子 -M
< / h4 >
< p >
ファイルの古さを調べたいときに、-Mを用いる。
< / p >
< h4 >
ファイルテスト演算子 -Tと-B
< / h4 >
< p >
「テキストファイルかどうかを調べる」のが-Tで、「バイナリファイルかどうかを調べる」のが-Bです。ちなみ日本語が含まれていると、「バイナリファイル」として認識されることに注意。
< / p >
< h4 >
ファイルグロッブ
< / h4 >
< p >
こいつを使うと、カレントディレクトリのファイルを簡単に列挙できる。使い方は
< / p >
< pre class = "syntax-highlight" >
< ワイルドカード>
< / pre >
< p >
となる。例は、
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > while< / span > (< *.txt> ) {
< span class = "synStatement" > print</ span > < span class = "synIdentifier" > $_</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< / pre >
< h4 >
練習問題 — その1
< / h4 >
< 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" > if< / span > (< span class = "synIdentifier" > @ARGV< / span > == < span class = "synConstant" > < / span > ) {
< span class = "synStatement" > print</ span > < span class = "synConstant" > 'Please give the argument.'</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
< span class = "synStatement" > print</ span > < span class = "synConstant" > 'For Example: perl temp.pl /'</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
< span class = "synStatement" > exit< / span > ;
}
< span class = "synComment" > # 引数を $arg に格納する< / span >
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > $arg< / span > = < span class = "synStatement" > shift< / span > ;
< span class = "synComment" > # 引数がディレクトリとして認識できる< / span >
< span class = "synStatement" > if< / span > (< span class = "synStatement" > -d< / span > < span class = "synIdentifier" > $arg< / span > ) {
< span class = "synComment" > # ディレクトリを開く< / span >
< span class = "synStatement" > opendir< / span > (< span class = "synIdentifier" > DIR< / span > , < span class = "synIdentifier" > $arg< / 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 = "synComment" > # ディレクトリの中を一つ一つ @file に格納する</ span >
< span class = "synStatement" > while< / span > (< span class = "synStatement" > my< / span > < span class = "synIdentifier" > $file< / span > = < span class = "synStatement" > readdir< / span > (< span class = "synIdentifier" > DIR< / span > )) {
< span class = "synComment" > # 親ディレクトリ・カレントディレクトリならば表示しない< / span >
< span class = "synStatement" > next< / span > < span class = "synStatement" > if< / span > (< span class = "synIdentifier" > $file< / span > < span class = "synStatement" > eq< / span > < span class = "synConstant" > '.'< / span > );
< span class = "synStatement" > next< / span > < span class = "synStatement" > if< / span > (< span class = "synIdentifier" > $file< / span > < span class = "synStatement" > eq< / span > < span class = "synConstant" > '..'< / span > );
< span class = "synComment" > # ファイル or ディレクトリを表示する< / span >
< span class = "synStatement" > print</ span > < span class = "synIdentifier" > $file</ span > , < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< span class = "synComment" > # きちんとディレクトリを閉じなきゃね< / span >
< span class = "synStatement" > closedir< / span > (< span class = "synIdentifier" > DIR< / span > );
}
< span class = "synComment" > # 引数がファイル名として認識可能< / span >
< span class = "synStatement" > if< / span > (< span class = "synStatement" > -f< / span > < span class = "synIdentifier" > $arg< / span > ) {
< span class = "synComment" > # ファイルを開く< / span >
< span class = "synStatement" > open< / span > (< span class = "synIdentifier" > FILE< / span > , < span class = "synIdentifier" > $arg< / 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 = "synComment" > # ファイルの内容を @f_content に格納する</ span >
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > @f_content< / span > = < span class = "synIdentifier" > < FILE> < / span > ;
< span class = "synComment" > # 一気に表示< / span >
< span class = "synStatement" > print< / span > < span class = "synIdentifier" > @f_content< / span > ;
< span class = "synComment" > # 閉じる< / span >
< span class = "synStatement" > close< / span > (< span class = "synIdentifier" > FILE< / span > );
}
< / pre >
< h4 >
練習問題 — その2
< / h4 >
< blockquote >
< p >
No. 0010 615
< / p >
< p >
No. 0009 602
< / p >
< p >
No. 0008 569
< / p >
< p >
No. 0007 549
< / p >
< p >
No. 0006 535
< / p >
< p >
No. 0005 514
< / p >
< p >
No. 0004 23
< / p >
< p >
No. 0003 19
< / p >
< p >
No. 0002 10
< / p >
< p >
No. 0001 4
< / p >
< p >
No. 0000 1
< / p >
< / blockquote >
< 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" > my< / span > < span class = "synIdentifier" > @nums< / span > = ();
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > @tmp_vols< / span > = ();
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > @tmp_nums< / span > = ();
< span class = "synComment" > # ファイルを開く< / span >
< span class = "synStatement" > open< / span > (< span class = "synIdentifier" > FILE< / span > , < span class = "synConstant" > 'mmlog.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" > $data< / span > = < span class = "synIdentifier" > < FILE> < / span > ) {
< span class = "synComment" > # 一行ずつ読み込んで@dataに格納< / span >
< span class = "synIdentifier" > @nums</ span > = < span class = "synStatement" > split</ span > (< span class = "synStatement" > /</ span >< span class = "synSpecial" > \s+</ span >< span class = "synStatement" > /</ span > , < span class = "synIdentifier" > $data</ span > );
< span class = "synComment" > # @tmp_volsと@tmp_numsに格納 </ span >
< span class = "synStatement" > push< / span > (< span class = "synIdentifier" > @tmp_vols< / span > , < span class = "synIdentifier" > $nums< / span > [< span class = "synConstant" > < / span > ] . < span class = "synIdentifier" > $nums< / span > [< span class = "synConstant" > 1< / span > ]);
< span class = "synStatement" > push< / span > (< span class = "synIdentifier" > @tmp_nums< / span > , < span class = "synIdentifier" > $nums< / span > [< span class = "synConstant" > 2< / span > ]);
}
< span class = "synComment" > # ファイルを閉じる< / span >
< span class = "synStatement" > close< / span > (< span class = "synIdentifier" > FILE< / span > );
< span class = "synComment" > # 実際に表示させる< / 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 > < < span class = "synIdentifier" > @tmp_vols< / span > - < span class = "synConstant" > 2< / span > ; < span class = "synIdentifier" > $i< / span > ++) {
< span class = "synStatement" > print</ span > < span class = "synIdentifier" > $tmp_vols</ span > [< span class = "synIdentifier" > $i</ span > ], < span class = "synConstant" > '-'</ span > , < span class = "synIdentifier" > $tmp_vols</ span > [< span class = "synIdentifier" > $i</ span > + < span class = "synConstant" > 1</ span > ], < span class = "synConstant" > ' --> '</ span > , < span class = "synIdentifier" > $tmp_nums</ span > [< span class = "synIdentifier" > $i</ span > ] - < span class = "synIdentifier" > $tmp_nums</ span > [< span class = "synIdentifier" > $i</ span > + < span class = "synConstant" > 1</ span > ], < span class = "synConstant" > " </ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< / pre >
2019-04-02 16:06:15 +00:00
< / div >