在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 |
请发表评论