2012年4月14日土曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の17章(上級テクニック), 17.6(練習問題)1を解いてみる。

1.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

my $file = './sample_text';
open FILE, $file or die "can't open $file: $!";
chomp(my @strs = <FILE>);
while(1){
  print "パターンを入力: ";
  chomp(my $pattern = <STDIN>);
  last if $pattern =~/^s*$/;
  my @result = eval{grep /$pattern/, @strs;};
  print "an error occured: $@" if $@;
  print @result + 0, "個\n";
  for(@result){
    print "$_\n";
  }
}

入出力結果(Terminal)

$ ./sample.pl
パターンを入力: ^f
4個
for fred and barney in lowercase, as we usually do. some lines
for repeated words, for example. (don't actually try to read this
for you."
fred had come up with a plan of his own. with his bowling ball in
パターンを入力: fred    
29個
for fred and barney in lowercase, as we usually do. some lines
mention a name like fred more than once so that you can search
one evening, fred and barney were getting ready to go bowling.
betty agreed. "barney and fred never think about us. we should
so wilma found fred and told him, "hey, fred, you've been going
"but wilma," said fred, "barney and i need to go tonight. this is
that wasn't good enough for wilma. "fred flintstone, if you think
discussion with betty. "i can't say no to you betty. but fred
"well, this is why fred and i have been practicing all week. but
you can't tell wilma what's going on, because fred made me
you and fred can sneak over there."
"i'll call fred and tell him our plan."
fred had come up with a plan of his own. with his bowling ball in
"nothing, nothing, dear," said fred, hurriedly hiding the bowling
"no problem. i've caught it now," lied fred. "i'll just take it
"uhm, black?" fred responded, holding the bowling ball behind his
"fred flintstone, you let me see that mouse this minute!" cried
the telephone rang. "you get that, wilma," said fred. "i'll be
mister fred flintstone. is mister fred flintstone available to
at that moment, fred rushed back into the house, having left the
bowling ball in the car. "it's for you, fred," said wilma.
"hello," fred said into the phone, not understanding.
"heya, fred," said barney. "betty and i have figured out what to
"yes, yes," said fred. wilma stood by, curious about this
wilma was getting more curious. fred said, "well, that mineral, i
"but fred," said barney, "it's a bowling ball, not a mineral."
"fred, where do you keep your bowling ball? is it in the garage?"
wilma interrupted him. "fred, give me the phone. i'll take care
she pulled the phone away from fred in time to hear barney say,
パターンを入力: 
$

0 コメント:

コメントを投稿