開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の17章(上級テクニック)、17.6(練習問題)1を解いてみる。
その他参考書籍
1.
コード(TextWrangler)
sample.pl
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.012; binmode STDOUT, ':utf8'; binmode STDIN, ':utf8'; my $file = 'sample_text'; open my $fh, "<", $file or die "can't open '$file': $!"; chomp(my @lines = <$fh>); while(1){ print "パターンを入力: "; chomp(my $pattern = <STDIN>); last if $pattern =~ /^\s*$/; my @matching_lines = eval { grep /$pattern/, @lines; }; if($@){ print "An error occured ($@)\n"; } else { print "個数: ", scalar @matching_lines, "\n"; if(@matching_lines){ print "マッチした文字列一覧\n"; for(@matching_lines){ print "$_\n"; } } } }
入出力結果(Terminal)
$ ./sample.pl パターンを入力: ( An error occured (Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at ./sample.pl line 19, <STDIN> line 1. ) パターンを入力: ^a 個数: 3 マッチした文字列一覧 as he headed again toward the door, wilma emerged from the at that moment, fred rushed back into the house, having left the after you're gone, betty and i will get your bowling ball and パターンを入力: ^b 個数: 14 マッチした文字列一覧 betty agreed. "barney and fred never think about us. we should bowling for the tenth night in a row, you've got another think be at the alley?" barney checked the time. "we aren't scheduled to start until half ball behind his back. "i think dino just saw a mouse." bathroom. "what color is it?" she asked. back as he walked backwards towards the door. back in a moment." he ducked outside. bowling ball in the car. "it's for you, fred," said wilma. bowling alley." barney continued. "you and wilma go ahead to the restaurant. bring it with us. where do you keep it?" barney here. no barney at all. put that bowling ball down, bamm-bamm!" パターンを入力: $
0 コメント:
コメントを投稿