要求:写一个名为greet的子函数,当给定一个名字作为参数时,打印出欢迎他的信息,并告诉之前已经迎接了哪些人。
如,如果运行
greet("a");
greet("b");
greet("c");
greet("d");
结果应该为:
Hi a, you are the first one here.
Hi b, I've seen: a.
Hi c, I've seen: a b.
Hi d, I've seen: a b c.
代码如下:
如,如果运行
greet("a");
greet("b");
greet("c");
greet("d");
结果应该为:
Hi a, you are the first one here.
Hi b, I've seen: a.
Hi c, I've seen: a b.
Hi d, I've seen: a b c.
代码如下:
- #! /usr/bin/perl
- sub greet {
- my ($na)=@_;
- @a=qw/a b c d e/;
- my ($n)=$#a;
- for (0..$n) {
- if ($na eq $a[$_]) {
- $m=$_;
- }
- }
- if ($m == 0) {
- print "Hi $a[$m], you are the first one here.\n";
- }
- else {
- my ($n2)=$m - 1;
- @b=undef;
- for (0..$n2) {
- push (@b,$a[$_]);
- }
- print "Hi $a[$m], I've seen: @b.\n";
- }
- }
- greet("a");
- greet("b");
- greet("c");
- greet("d");
编辑回复