$#_ the index of the last element.
@_ the count of the elements.
- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
my @fred = qw{ 1 3 5 7 9 };
-
#my $fred_total = total(@fred);
-
my $fred_total = total((1..1000));
-
print "The total of \@fred is $fred_total.\n";
-
#print "Enter some numbers on separate lines: ";
-
#my $user_total = total(<STDIN>);
-
#print "The total of those numbers is $user_total.\n";
-
my @above = above_average((0..10));
-
print "the above is @above\n";
-
-
sub total{
-
my $sum;
-
-
foreach (@_){
-
$sum += $_;
-
}
-
$sum;
-
}
-
-
sub above_average{
-
my $average = total(@_)/@_;
-
my @list;
-
-
foreach (@_){
-
if ($_ > $average){
-
push @list, $_ ;
-
}
-
}
-
@list;
-
}
阅读(334) | 评论(0) | 转发(0) |