Perl realizes high watermark algorithm

Source: Internet
Author: User
Tags foreach first row

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.