给出一个数字数组,元素个数未知,通过子程序 sub max {} 来返回最大值。结果为
The max of (1 99 2 45 10) is 99.
- #! /usr/bin/perl
- sub max {
- my ($m)=shift @_;
- for (@_) {
- if ($m < $_) {
- $m=$_;
- }
- }
- $m;
- }
- @a=qw /1 99 2 45 10/;
- $max=&max(@a);
- print "The max of (@a) is $max.\n";
The max of (1 99 2 45 10) is 99.
编辑回复