我在 Perl 中的问题:编写带有子例程的 Perl 脚本,该子例程接受值列表并将最大值、最小值和平均值返回给调用程序。
#!/usr/bin/perl
sub large_and_small {
my (@numbers);
@numbers = @_;
my ($small, $large);
$large = $numbers[0];
$small = $numbers[0];
foreach my $i (@numbers) {
if ($i > $large) {
$large = $i;
}
elsif ($i < $small) {
$small = $i;
}
}
return ($small, $large);
}
sub avg {
my ($avg);
my ($total);
foreach (@test_array) {
$total += $_;
}
$avg = $total/scalar @test_array;
return $avg;
}
my (@test_array, @ret);
@test_array = (15, 5, 7, 3, 9, 1, 20,1 3, 9, 8, 15, 16, 2, 6, 12, 90);
@ret = large_and_small(@test_array);
print "The Largest value is ", $ret[1], "\n";
print "The Smallest value is ", $ret[0], "\n";
print "The Average value is", avg(@test_array), "\n";
我得到的输出是:
The Largest value is 90
The Smallest value is 1
Illegal division by zero at /tmp/135044395416028.pl line 59.
我在脚本中哪里出错了?请帮助。在此先感谢