Chinaunix首页 | 论坛 | 博客
  • 博客访问: 570395
  • 博文数量: 201
  • 博客积分: 7734
  • 博客等级: 少将
  • 技术积分: 1994
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-09 19:18
文章分类

全部博文(201)

文章存档

2011年(28)

2010年(173)

分类:

2010-07-20 11:46:22

数组的应用:

#!/usr/bin env perl

@zeng = `top -b -n 1`;
foreach (@zeng) {
print "$_\n";
}


say的用法:可以自动换行

#!/user/bin env perl

use 5.010; #表示使用perl 5.10的版本。
say "Hello World!";



my @fred = above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";
my @barney = above_average(100, 1..10);
print "\@barney is @barney\n";
print "(Should be just 100)\n";

sub average {
  if (@_ == 0) { return }
  my $count = @_;
  my $sum = total(@_);
  $sum/$count;
  }
  
  sub above_average {
    my $average = average(@_);
    my @list;
    foreach my $element (@_); {
     if ($element > $average) {
        push @list, $element;
    }
  }
  @list;
  }
==================================================
#!/usr/bin/perl -w

use 5.010;
greet( 'Fred');
greet( 'barney');
sub greet {
  state $last_person;
  my $name = shift;
  print "Hi $name!";
  if ( defined $last_person ) {
    print "$last_person is also here!\n";
    }
    else {
        print "You are the first one here!\n";
        }
        $last_person = $name;
        }
===================================================
greet( 'Fred' );
greet( 'barney' );
greet( 'wilma' );
greet( 'Betty' );

sub greet {
    state @names;
    my $name = shift;
    print "Hi $name!";
    if ( @names ) {
        print "I've seen: @names\n";
        }
    else {
        print "You are the first one here!\n";
        }
        push @names, $name;
        }


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