Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752140
  • 博文数量: 79
  • 博客积分: 2671
  • 博客等级: 少校
  • 技术积分: 1247
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-02 15:26
个人简介

宅男

文章分类

全部博文(79)

文章存档

2017年(11)

2016年(12)

2015年(6)

2012年(10)

2011年(33)

2010年(7)

分类: 其他平台

2017-02-17 14:09:55

1-3.

  1. sub total {
  2.         my $sum = shift @_;

  3.         foreach $a (@_) {
  4.                 $sum += $a;
  5.         }

  6.         return $sum;
  7. }

  8. sub average {
  9.         my($sum, $number) = @_;

  10.         return $sum/$number;
  11. }

  12. #my @fred = 1..1000;
  13. #my $fred_total = total(@fred);
  14. #print "The total is $fred_total\n";

  15. sub above_average {
  16.         my @numbers = @_;
  17.         my $number = @numbers;
  18.         my $mean = average(total(@numbers),$number);
  19.         my @out = ();

  20.         foreach $a (@_) {
  21.                 if ($a > $mean) {
  22.                         push(@out, $a);
  23.                 }
  24.         }

  25.         return @out;
  26. }


  27. my @fred = above_average(1..10);
  28. print"\@fred is @fred\n"
4.


  1. my $prev_person = undef;

  2. sub greet {
  3.         my ($person) = @_;

  4.         if (!defined($prev_person)) {
  5.                 print "Hi, $person! You the first one here!\n";
  6.         } else {
  7.                 print "Hi, $person! $prev_person is also here!\n";
  8.         }
  9.         $prev_person = $person;
  10. }

  11. my @persons = ();
  12. sub greet2 {
  13.         my ($person) = @_;

  14.         if (@persons == 0) {
  15.                 print "Hi, $person! You the first one here!\n";
  16.         } else {
  17.                 print "Hi, $person! I've seen: @persons \n";
  18.         }
  19.         push (@persons, $person);
  20. }
  21. greet2("Fred");
  22. greet2("Barney");
  23. greet2("zhenhua")

阅读(722) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~