Perl implements high waterline Algorithms

Source: Internet
Author: User

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.

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.