1. Question
There are some problems described in the original question. Question 1st: match the row containing fred. Question 2nd: match the rows containing fred or Fred. Question 3rd: match the row containing the vertex number. Question 4th: match the rows starting with an upper-case letter. Question 5th: contains two consecutive rows with the same non-space characters, such as AA, aa, ¥, and so on. Question 6th: Match rows with wilma and fred.
2. Code and Output
Question 1st: match the row containing fred.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-1
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/fred /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 #/fred/match a string containing fred
15 #/^ fred $/match strings that contain only fred
16 #-----------------------------------------------------------#
Question 2nd: match the rows containing fred or Fred.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-2
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/[f, F] red /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 #/fred | Fred/
15 #-----------------------------------------------------------#
Question 3rd: match the row containing the vertex number.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-3
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/\./){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
Question 4th: match the rows starting with an upper-case letter.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-4
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/^ [A-Z]/) {
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
Question 5th: contains two consecutive rows with the same non-space characters, such as AA, aa, ¥, etc.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-5
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/(\ S) \ 1 /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 # actually match the BB, aa, 33, --, two identical consecutive characters instead of finding
15 # longest continuous symmetric substring in a string
16 #-----------------------------------------------------------#
Question 6th: Match rows with wilma and fred.
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-6
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/wilma. * fred | fred. * wilma /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 # In fact, it is better to use two if statements to determine whether a row exists, fred and wilma, because
15 #. Assume that the row's fred and wilma do not overlap correctly. The two words are not repeated.
The 16 # character. To match abb and bba, abba cannot be
17 #/abb. * bba | bba. * abb/matched, but can be obtained through if (/abb /){
18 # if (/bba/) {}} matches.
19 #-----------------------------------------------------------#
3. File
/Files/pangxiaodong/learns ningperl/ch7-answer.rar