18. ぼくはスゴくないので
こうします…😅
#! /usr/bin/perl
#
# sumDir.pl -- 任意のディレクトリ配下のファイルを拡張子ごとに集計する
use 5.010;
#use strict;
use warnings;
use File::Find;
use File::Basename;
find sub {$h{(fileparse($_,qw{.[^.]+$}))[2]}++ if -f},@ARGV;
END {
map{
print "$_:$h{$_}n";
}keys %h
}
19. 実際にはこうします…😅
#! /usr/bin/perl
#
# sumDir2.pl -- 任意のディレクトリ配下のファイルを拡張子単位に集計する
use 5.010;
use strict;
use warnings;
use File::Find;
use File::Basename;
my $dir = shift;
my %hash = ();
find &file_proc, $dir; #(つづく)
20. 実際にはこうします…😅
#(つづき)
for (sort keys %hash) {
say "$_:$hash{$_}";
}
sub file_proc {
return unless -f $_;
# http://d.hatena.ne.jp/perlcodesample/20100225/1264257759
my ($basename, $dirname, $ext) = fileparse($_, qr/..*$/);
$hash{$ext} += 1;
}
34. 例2:Date::Simple(モジュールの
シノプシスを動かす!)
#! /usr/bin/perl
#
# dateSimpleTest.pl --- Date::Simpleのテスト
use 5.010;
use strict;
use warnings;
use Date::Simple ('date', 'today');
# Difference in days between two dates:
my $diff = date('2017-03-04') - date('2017-01-01');
say "今年に入ってから $diff 日が経った";
# Offset $n days from now:
my $date = today() + 100;
say "今日の100日後は $date"; # uses ISO 8601 format (YYYY-MM-DD)
他にも、技術書の
コードスニペットを
動くようにする
というのも考えられる…
36. 例3:正規表現テスター#! /usr/bin/perl
#
# regTest.pl -- 正規表現テスター
use 5.010;
use strict;
use warnings;
use utf8;
binmode DATA, ":encoding(UTF-8)";
binmode STDOUT, ":encoding(UTF-8)";
while (<DATA>) {
print if /Team-A/; # Team Aの人
}
# 順位 t 苗字 sp 名前 t メールアドレス t チーム名 t グループ名n
# メール アドレスは「ローマ字名_ローマ字苗字@example.com」(※架空のもので
す)
__DATA__ (つづく!)
43. MS Officeとの共存(キセル方式)
エクスポートするVBA
Private Sub CommandButton1_Click()
Dim files(100)
mypath = Environ("UserProfile") & "Desktopfoo"
files(1) = Dir(mypath & "*.docx")
i = 1
Do While files(i) <> ""
i = i + 1
files(i) = Dir()
Loop
i = 1
Do While files(i) <> ""
Documents.Open FileName:=mypath & files(i)
‘ここで何かやってもいいが、何もせず保存している
ActiveDocument.SaveAs2 FileName:=mypath & files(i) & ".XML", fileformat:=wdFormatXML
ActiveDocument.Close
i = i + 1
Loop
End Sub
46. MS Officeとの共存(キセル方式)
インポートするVBA
Private Sub CommandButton11_Click()
Dim files(100)
mypath = Environ("UserProfile") & "Desktopfoo"
files(1) = Dir(mypath & "*.xml")
i = 1
Do While files(i) <> ""
i = i + 1
files(i) = Dir()
Loop
i = 1
Do While files(i) <> ""
Documents.Open FileName:=mypath & files(i)
‘何もせず保存するだけ
ActiveDocument.SaveAs2 FileName:=mypath & files(i) & ".DOCX", fileformat:=wdFormatXMLDocument
ActiveDocument.Close
i = i + 1
Loop
End Sub