1. 題目
2. 代碼與輸出
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter9,exercise-1
3 # Date: 2012-01-19
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 @array = qw {
8 fredfredfred
9 fredfredbarney
10 barneyfredfred
11 barneybarneybarney
12 fred|barney
13 };
14 @content = qw {
15 fred fred|barney
16 };
17 foreach $what(@content) {
18 say "\$what = $what";
19 say "\@array = (@array)";
20 foreach $text(@array) {
21 if($text =~ m/(?:$what){3}/) {
22 say "<$`><$&><$'>";
23 }
24 }
25 say "----------------------------";
26 }
27 <STDIN>;
28 #-----------------------------------------------------------#
29 # 將某個字串重複3次,注意這個字串可能包含或|這樣的字元,因此將其用括弧
30 # 括起來就好了
31 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter9,exercise-2
3 # Date: 2012-01-19
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $input_file = $ARGV[0];
8 $output_file = $ARGV[0] . ".out";
9 unless(!open IN, "<$input_file") {
10 say "Can't read <$input_file> !";
11 }
12 unless(open OUT, ">$output_file") {
13 say "Can't write <$output_file> !";
14 }
15 while(<IN>) {
16 s/Fred/Larry/gi;
17 print OUT $_;
18 }
19 #-----------------------------------------------------------#
20 # 唯讀<,唯寫>,追加寫>>,預設就是讀寫
21 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter9,exercise-3
3 # Date: 2012-01-19
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $text = "fred&wilma&fred&wilma&fred&wima";
8 say "text = $text";
9 @array = split /fred/gi, $text;
10 foreach(@array) {
11 s/wilma/Fred/gi;
12 }
13 $text = join Wilma, @array;
14 say "text = $text";
15 <STDIN>;
16 #-----------------------------------------------------------#
17 #1.該解法是用Fred作為分割符,將字串分開,然後,將分開的
18 # 字串中的wilma環衛Fred,然後再用Wilma將分開的字串連起來
19 #2.書中解法是,首先對字串chomp,保證不含\n,然後將Fred都換為
20 # \n,接著講Wilma都換為Fred,最後將\n都換為Wilma
21 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter9,exercise-4
3 # Date: 2012-01-19
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $^I = ".bak";
8 $text = "## Copyright (C) 2012 by Yours Truly\n";
9 while(<>) {
10 if(/#!/) {
11 $_ = $_ . $text;
12 }
13 print $_;
14 }
15 #-----------------------------------------------------------#
16 # 1. /i表示忽略大小寫,/g表示全域匹配
17 # 2. $^I,在讀入檔案假設是word.txt,首先將其改名為word.txt.out,
18 # 然後建立一個空的word.txt檔案,接著,word.txt.out的每一行,
19 # 並且通過print函數寫入到word.txt檔案中。
20 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter9,exercise-4
3 # Date: 2012-01-19
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $text = "## Copyright (C) 2012 by Yours Truly\n";
8 foreach(@ARGV) {
9 $files{$_} = 1;
10 }
11 while(<>) {
12 if(/## Copyright/) {
13 delete $files{$ARGV};
14 }
15 }
16 @ARGV = keys %files;
17 $^I = ".bak";
18 if(@ARGV != 0) {
19 while(<>) {
20 if(/#!/) {
21 $_ = $_ . $text;
22 }
23 print $_;
24 }
25 }
26 #-----------------------------------------------------------#
27 # 1. /i表示忽略大小寫,/g表示全域匹配
28 # 2. $^I,在讀入檔案假設是word.txt,首先將其改名為word.txt.out,
29 # 然後建立一個空的word.txt檔案,接著,word.txt.out的每一行,
30 # 並且通過print函數寫入到word.txt檔案中。
31 #-----------------------------------------------------------#
輸出略,與前一題類似。
3. 檔案
/Files/pangxiaodong/LearningPerl/ch9-answer.rar