Perl-Chapter 3-list and array-Exercises

Source: Internet
Author: User

1. Question

2. Code

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter3
3 # Date: 2012-01-13
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 # code-1 #
7 #-----------------------------------------------------------#
8 $ split = "-------------------------------------------------";
9 $ code1 = 'chomp (@ strs = <STDIN>); '. "\ n ".
10 'foreach (reverse (@ strs) {'. "\ n ".
11 'print $ _. "\ n"; '. "\ n ".
12 '}';
13 print "code-1 \ n". $ code1. "\ n". $ split;
14 #-----------------------------------------------------------#
15 # execute-1 #
16 #-----------------------------------------------------------#
17 print "\ nexecute-1 \ n ";
18 chomp (@ STRs = <stdin> );
19 foreach (reverse (@ STRs )){
20 Print $ _. "\ n ";
21}
22 #-----------------------------------------------------------#
23 # code-2 #
24 #-----------------------------------------------------------#
25 $ code2 = '@ names = QW {Fred Betty Barney Dino Wilma pebbles Bamm-Bamm};'. "\ n ".
26 'chomp (@ indexes = <STDIN>); '. "\ n ".
27 'foreach (@ indexes) {'. "\ n ".
28 'print @ names [($ _-1) % 7]. "\ n"; '. "\ n ".
29 '}';
30 print $ split. "\ ncode-2 \ n". $ code2. "\ n". $ split;
31 #-----------------------------------------------------------#
32 # execute-2 #
33 #-----------------------------------------------------------#
34 print "\ nexecute-2 \ n ";
35 @ names = qw {fred betty barney dino wilma pebbles bamm-bamm };
36 chomp (@ indexes = <STDIN> );
37 foreach (@ indexes ){
38 print @ names [($ _-1) % 7]. "\ n ";
39}
40 #-----------------------------------------------------------#
41 # code-3 #
42 #-----------------------------------------------------------#
43 $ code3 = 'chomp (@ strs = <STDIN>); '. "\ n ".
44 'foreach (sort (@ strs) {'. "\ n ".
45 'print $ _. ""; '. "\ n ".
46 '}'. "\ n ".
47 'print "\ n"; '. "\ n ".
48 'foreach (sort (@ strs) {'. "\ n ".
49 'print $ _. "\ n"; '. "\ n ".
50 '}';
51 print $ split. "\ ncode-3 \ n". $ code3. "\ n". $ split;
52 #-----------------------------------------------------------#
53 # execute-3 #
54 #-----------------------------------------------------------#
55 print "\ nexecute-3 \ n ";
56 chomp (@ STRs = <stdin> );
57 foreach (sort (@ STRs )){
58 Print $ _."";
59}
60 print "\ n ";
61 foreach (sort (@ STRs )){
62 Print $ _. "\ n ";
63}
64 #-----------------------------------------------------------#
65 print "end !!! ";
66 <STDIN>
67 #-----------------------------------------------------------#
68 # conclusion
69 #1. @ str_array = <STDIN>; each time a row is read, an element is added to the array,
70 # Until the Read File is empty or enter Control + Z (Windows), Control + D (Unix)
71 #2. $ _ indicates variables with omitted parameters, such as foreach () and print.
72 #3. reverse, sort, these functions are different from chomp, more like the return value, while chomp directly modifies the original array or the original variable

3. Output

4. File

/Files/pangxiaodong/learns ningperl/ch3-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.