File::DirListの使い方。
ディレクトリに含まれるファイルリストを返す。
SYNOPSIS
use File::DirList; # my @list = File::DirList::list('.', 'dn', 1, 0);
DESCRIPTION
このモジュールは2つのメソッドを持つ。
list($dirName, $sortMode, $noLinks, $hideDotFiles, $showSelf)
指定したディレクトリに含まれるファイルのリストを返す。
- $dirName : ディレクトリ名
- $sortMode : ソートモード
- d or D
- "Directory" sort. 'd' means all the directories will precede files, 'D' means revese.
- n or N
- Sort by file (or subdir) name. 'n' means strate, 'N' means revese
- i or I
- Same as 'n' but case insesitive
- m or M
- Sort by modification time, strate or revese
- c or C
- Sort by creation time, strate or revese
- a or A
- Sort by access time, strate or revese
- s or S
- Sort by access time, strate or revese
- $noLinks
If true symbolic links will not be examined. Set it on the platforms without symlink support.
- $hideDotFiles
If true 'dot' files will not be reported.
- $showSelf
If true '.' directory entry will be reported.
Array elemnts are refferences to another arrays representing an item.
Item array contains 17 elements:
- [0..12]
- Result of stat() for this item. For valid symbolik links stat of the target item is returned.
- [13]
- Name of the item.
- [14]
- Is item a directory? Contains 0 for non-directory items, 1 for directories, 2 for '..', 3 for '.'. Used by "d or D" sorting.
- [15]
- Is item a link? 0 for non-links, 1 for valid links, -1 for invalid links.
- [16]
- Link target. undef for non-links, target path for links.
[15] and [16] are set to non-link if $examineLinks is false.
sortList($list, $sortMode)
list()により作られたリストを再ソートする。
ソートされたリストを返す。
- $lis
Reference to a list produced by list()
- $sortMode
Sorting rules.
File::Copyの使い方。
SYNOPSISのままです。
use File::Copy; copy("file1","file2") or die "Copy failed: $!"; copy("Copy.pm",\*STDOUT); move("/dev1/fileA","/dev2/fileB"); use File::Copy "cp"; $n = FileHandle->new("/a/file","r"); cp($n,"x");
Text::CSV::Simpleの使い方。
NAME
Text::CSV::Simple
METHODS
read_file
my @data = $parser->read_file($filename);
$filenameにCSVファイルパスを与える。
ファイルの内容を解析し,リストでデータを返す。
want_fields
$parser->want_fields(1, 2, 4, 8);
read_fileをする前に,欲しているデータ列番号を設定しておく。
こうしておくと,設定した番号の列のみリストに入れられる。
番号は0から。
fiels_map
$parser->field_map(qw/id name null town null postcode/);
読み込むデータ列に名前をつけておくことができる。
これをすると各データがハッシュに記録される。
EXAMPLE
#!/usr/bin/perl use Text::CSV::Simple; my $parser = Text::CSV::Simple->new({binary => 1}); # binaryを1にしておくことで日本語もOK my $filename = './hoge.csv'; my @csv = $parser->read_file($filename); foreach(@csv){ print join("\t",@csv), "\n"; } # CSVデータを読み込みタブで繋げる print $csv[0]->[0], "\n"; # 1行,1列目のデータの表示 $parser->want_fields(0, 3); @csv = $parser->read_file($filename); # 1列目,4列目のデータのみを得る $parser->field_map(qw/date val1 val2 val3 val4/); @csv = $parser->read_file($filename); print $csv[9]->{'date'}, "\t", $csv[9]->{'val3'}, "\n"; # 10行目の日付と値3をフィールド名を使ってアクセス
PerlでCSVを扱うためのモジュール探し
open IN, "<", $filename; while(<IN>){ my @col = split(/,/, $_); ... }
をするのが面倒なのでcpanを漁ってみる。
いつもお世話になっているGoogle先生,および最速インターフェース研究会様でモジュール検索。
調べた結果,Text::CSV_XSを使うみたい。
とりあえずインストールしてみる。
cpanシェルを起動し,以下のコマンドを実行。
install Text::CSV_XS
問題なくインストール完了。はてしなく便利>cpan。
で,使い方を読んでると・・・あれ?なんか違くね?
どうやらText::CSV_XSは上記CSV処理コードのsplitを担当するものらしい。
splitでは二重引用符とかの処理がうまくできないからText::CSV_XSを使うみたい。
splitで問題ないCSVファイルを使っている私には必要ないモジュールであることを認識。
私が欲しいのはCSVファイルを指定して全体をメモリ上に読み込んでくれるモジュールです。
てことで再検索。
すぐ近くにText::CSV::Simpleというモジュールを発見。
同様に
install Text::CSV install Text::CSV::Simple
インストール完了。やっぱり便利>cpan。
これは良さそう。
こいつの特徴は
- ファイル名を指定すれば,ファイルを読み込み解析する
- 解析したデータをリストへ出力する
- 出力されたリストは2次元配列のように扱うことができる
- 各列にフィールド名をつけることもでき,そのフィールド名でアクセスすることができる
- 必要なデータ列のみを抜きだすことができる
ようだ。
これの使い方はText::CSV::Simpleの使い方。にまとめる。
Statistics::Basicの使い方
解説そのままだけど,毎回英語読むのはしんどいので書いておく。
NAME
とっても基本的な統計関数だよ!!
SYNOPSYS
こんな関数があるよ!!
# ひとつのベクトルに対するメソッド Statistics::Basic::Mean; # 平均 Statistics::Basic::Median; # 中央値 Statistics::Basic::Mode; # 最頻値 Statistics::Basic::Variance; # 分散 Statistics::Basic::StdDev; # 標準偏差 # ふたつのベクトルに対するメソッド Statistics::Basic::CoVariance; # 共分散 Statistics::Basic::Correlation; # 相関
EXAMPLE
例はもうそのままだ!手抜きです
my $mean = Statistics::Basic::Mean->new($array_ref)->query; print "$mean\n"; # hooray # That works, but I needed to calculate a LOT of means for a lot of # arrays of the same size. Furthermore, I needed them to operate FIFO # style. So, they do: my $mo = new Statistics::Basic::Mean([1..3]); print $mo->query, "\n"; # the avearge of 1, 2, 3 is 2 $mo->insert(4); # Keeps the vector the same size automatically print $mo->query, "\n"; # so, the average of 2, 3, 4 is 3 # You might need to keep a running average, so I included a growing # insert $mo->ginsert(5); # Expands the vector size by one and appends a 5 print $mo->query, "\n"; # so, the average is of 2, 3, 4, 5 is 7/2 # And last, you might need the mean of [3, 7] after all the above $mo->set_vector([2,3]); # *poof*, the vector is 2, 3! print $mo->query, "\n"; # and the average is now 5/2! Tadda! # These functions all work pretty much the same for ::StdDev and # ::Variance but they work slightly differently for CoVariance and # Correlation. # Not suprisingly, the correlation of [1..3] and [1..3] is 1.0 my $co = new Statistics::Basic::Correlation( [1..3], [1..3] ); print $co->query, "\n"; # Cut the correlation of [1..3, 7] and [1..3, 5] is less than 1 $co->ginsert( 7, 5 ); print $co->query, "\n";
CPANシェルの起動
root権限で以下のコマンドを実行。
# perl -MCPAN -e shell
特殊リテラル
特殊リテラル | 説明 |
---|---|
__END__ | プログラムの論理的な終わり。これ以降は無視される。ただし,DATAファイルハンドルで読み込むことができる。 |
__FILE__ | 現在のファイル名。 |
__LINE__ | 現在の行番号 |
__PACHAGE__ | 現在のパッケージ名 |
特殊リテラルを用いたプログラム内のデータ読込み
#!/user/local/bin/perl .... # 必要な処理 # なんらかしらの条件により、__END__ 以降の文章を出力 if ( ... ) { @list = <DATA>; print @list; } __END__ Document since "__END__"....