Perl implements high waterline Algorithms
This article mainly introduces Perl's implementation of high-water line algorithms (methods to solve multi-value comparison problems). From the code examples in this article, you can also learn about array traversal, function writing, function calling, and other knowledge, for more information, see
"High Water Line" algorithm: after a great deal of water, when the last wave fades, the high water line will indicate the highest water level you have ever seen.
Next, let's take a look at the application of the "high waterline" algorithm 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 @ _; # The first value in the array, which is regarded as the maximum value temporarily. Foreach (@ _) {# traverse the array @_ If ($ _> $ max_so_far) {# Check whether other elements have values greater than $ max_so_far. $ Max_so_far =$ _;} # update the maximum variable if any } $ Max_so_far; } My $ _ MaxData = & max (2, 3, 8, 5, 10 ); Print $ _ MaxData; |
In the first line, shift the array @ _ and place element 2 in the maximum $ max_so_far variable. The remaining elements in @ _ are ), then, use foreach to traverse the array. The first element 3 in the new array is greater than 2, and is moved to the $ max_so_far variable, and so on. The last 10 is the largest element in the array.