Learning Perl学习笔记(2)第三章Lists and

2020-10-22  本文已影响0人  生信start_site

foreach Control Structure

遍历数组中的每一个元素,然后执行花括号里的命令:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
foreach my$rock (qw/ bedrock slate lava /) {
        print "One rock is $rock.\n";
}
$ ./practice.pl
One rock is bedrock.
One rock is slate.
One rock is lava.

Forcing Scalar Context

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
my@rocks = qw( talc quartz jade obsidian );
print "How many rocks do you have?\n";
print "I have ",scalar @rocks, " rocks!\n"; #计算数组里有多少个元素
$ ./practice.pl
How many rocks do you have?
I have 4 rocks!

第三章课后练习:

(1)第一题:

Write a program that reads a list of strings on separate lines until end-ofinput
and prints out the list in reverse order. If the input comes from the keyboard,
you’ll probably need to signal the end of the input by pressing Control-D
on Unix, or Control-Z on Windows.

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
print "Enter some strings,and press ctrl_D:\n";
my@enterstrings = <STDIN>;
my@reverse_word = reverse @enterstrings;
print @reverse_word;
$ ./practice.pl
Enter some strings,and press ctrl_D:
I
am
Yan
Yan
am
I

(2)第二题

Write a program that reads a list of numbers (on separate lines) until end-ofinput
and then prints for each number the corresponding person’s name from the
following list. (Hardcode this list of names into your program. That is, it should appear in your program’s source code.) For example, if the input numbers were 1,
2, 4, and 2, the output names would be fred, betty, dino, and betty:
fred betty barney dino wilma pebbles bamm-bamm

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
my@names = qw/ fred betty barney dino wilma pebbles bamm-bamm /;
print "Whose name you want to know,please enter number, then press Ctrl-D:\n";
chomp(my@numbers = <STDIN>);
foreach (@numbers) {
        print "$names[$_ - 1]\n";
}
$ ./practice.pl
Whose name you want to know,please enter number, then press Ctrl-D:
5
1
2
wilma
fred
betty

(3)第三题

Write a program that reads a list of strings (on separate lines) until end-ofinput.
Then it should print the strings in code point order. That is, if you enter
the strings fred, barney, wilma, betty, the output should show barney betty
fred wilma. Are all of the strings on one line in the output or on separate lines?
Could you make the output appear in either style?

输出结果显示在同一行:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
print "Please enter some strings,then press ctrl_D:\n";
chomp(my@input_strings = <STDIN>);
my@sorted_strings = sort(@input_strings);
print "@sorted_strings\n";
$ ./practice.pl
Please enter some strings,then press ctrl_D:
zeb1
snai1
brca2
brca2 snai1 zeb1

输出结果显示在不同行:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
print "Please enter some strings,then press ctrl_D:\n";
my@input_strings = <STDIN>;
my@sorted_strings = sort(@input_strings);
print "The sorted result is:\n"."@sorted_strings";
$ ./practice.pl
Please enter some strings,then press ctrl_D:
zeb1
snai1
brca2
The sorted result is:
brca2
 snai1
 zeb1
上一篇下一篇

猜你喜欢

热点阅读