Getting started with Perl-Chapter 9-using regular expressions to process text-Exercises

Source: Internet
Author: User

1. Question

2. Code and Output

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. fredfred
9 fredfredbarney
10 barneyfredfred
11 barneybarneybarney
12 Fred | barney
13 };
14 @ content = QW {
15 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 # repeat a string three times. Note that this string may contain or | such a character, so it is enclosed in parentheses.
30 # It should be included.
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 # Read-Only <, write only>, append write>. The default value is read/write.
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 & 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. This solution uses Fred as the delimiter to separate strings.
18 # Wilma sanitation Fred in the string, and then use wilma to connect the separate string
19 #2. The solution in the book is to replace Fred
20 # \ n. Then, replace wilma with Fred and \ n with 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 indicates case-insensitive,/G indicates global match
17 #2. Change the file name to word.txt. out first,
18 # After creating an empty word.txt file, connect each line of word.txt. Out,
19 # And write it to the word.txt file through printlinks.
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 indicates case-insensitive,/G indicates global match
28 #2. Change the file name to word.txt. out first,
29 # After creating an empty word.txt file, connect each line of word.txt. Out,
30 # And write it into the word.txt file through printlinks.
31 #-----------------------------------------------------------#

Output omitted, similar to the previous question.

3. File

/Files/pangxiaodong/learns ningperl/ch9-answer.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.