This article mainly introduces the Perl implementation of high watermark algorithm (to solve the multivalued comparison problem method), from the code examples can also learn to array traversal, function writing, function calls and other knowledge, the need for friends can refer to the
"High watermark" algorithm: After the flood, the last wave subsided, the high waterline will indicate the highest water level seen.
Here's a look at the "high watermark" algorithm used in Perl.
?
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
#! /usr/bin/perl; Use UTF8; Sub Max {my ($max _so_far) = Shift @_; #数组中第一个值, temporarily as the maximum value. foreach (@_) {#遍历数组 @_ if ($_> $max _so_far) {#看其它元素是否有比 $max _so_far large value. $max _so_far = $_;} #如果有话, update the maximum value variable} $max _so_far; My $_maxdata = &max (2,3,8,5,10); Print $_maxdata; |
The first row of the array @_ is shifted, placing an element 2 in the maximum $max_so_far variable, the remaining element in the @_ is (3,8,5,10), then looping through the arrays with a foreach loop, the first element in the new array is 3:2 large, and is moved to the $max_so_ In the far variable, and so on, the last 10 is the largest element in the array.