Perl List: instance used by Util Module

Source: Internet
Author: User

Perl List: instance used by Util Module

This article mainly introduces the Perl List: Util module instance, this article provides some practical functional code for scanning a list that meets the condition and obtaining the first matching sum between 1 and 1000, and obtaining the maximum and minimum values of a set of numbers, for more information, see

There are some modules specifically used in Perl to Process List data, such as List: Util module. This module is included in the standard library and provides various efficient common List processing tools. Because it is implemented in C language, the speed is generally quite fast!

[Example 01] scan a list that meets the conditions and retrieve the first qualified

General Practice:

The Code is as follows:

Use 5.010;

My @ names = qw (Wendy Jerry Betty Wendy Alice );

Foreach (@ names ){

If (/\ bWendy \ B/I ){

$ Match =$ _;

Last;

}

}

Say $ match;

If you use the first subroutine provided by the List: Util module, it is much simpler.

The Code is as follows:

Use List: Util qw (first );

My $ match = first {/\ bWendy \ B/I} @ names; # terminate when the first Wendy is found

If you change to a number, for example

[Example 02] calculate the sum between 1 and 1000.

General Practice:

The Code is as follows:

Use 5.010;

My $ total = 0;

Foreach (1 .. 1000 ){

$ Total + = $ _;

}

Say $ total; # result 500500

If you use the sum subroutine provided by the List: Util module, it is also very simple:

The Code is as follows:

Use List: Util qw (sum );

My $ total = sum (1 .. 1000); # result 500500

[Example 03] calculate the maximum and minimum values of a group of numbers.

General Practice:

The Code is as follows:

#! /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; # The result is 10.

If you use the max subroutine provided by the List: Util module, it is very simple:

The Code is as follows:

Use List: Util qw (max );

My $ max = max (2, 3, 8, 5, 10 );

Print $ max; # The result is 10.

Similarly, the min subroutine provided by the List: Util module can be used to obtain the minimum value:

The Code is as follows:

Use List: Util qw (min );

My $ min = min (2, 3, 8, 5, 10); # the minimum value is 2.

[Example 04] Sort a group of strings

If you use the conventional method, you must compare them one by one in sequence. You can use the List: Util maxstr subroutine to easily implement the following:

The Code is as follows:

Use List: Util qw (maxstr );

My $ max_str = maxstr (qw/Jerry Betty Alice Fred Barney jerry /);

Print $ max_str;

[Example 05] random sorting of elements in the list

If you use a conventional method, it is very difficult to implement it, but using the shuffle subroutine in List: Util is very simple, just a command!

The Code is as follows:

Use List: Util qw (shuffle );

My @ shuffled_nums = shuffle (1 .. 10); #3 9 8 5 6 4 1 10 2 7

My @ shuffled_name = shuffle ('A' .. 'G'); # F E G A B D

[Example 06] Check whether an element exists in the List, whether any element exists, or whether all elements meet the conditions. Supports grep-like syntax

If you use the conventional method, it is difficult to implement it, but using List: MoreUtils is also easy to implement. The Code is as follows:

The Code is as follows:

Use List: MoreUtils qw (none any all );

My @ numbers = qw (7 4 1 3 78 );

If (none {$ _> 100} @ numbers) {print "No elements over 100 \ n ";}

Elsif (any {$ _> 50} @ numbers) {print "Some elements over 50 \ n ";}

Elsif (all {$ _ <10} @ numbers) {print "All elements <10 \ n ";}

NOTE: List: MoreUtils is not a built-in module and needs to be downloaded.

[Example 07] process multiple name lists at the same time and retrieve two digits at a time

The Code is as follows:

Use List: MoreUtils qw (natatime );

My @ names_1 = qw (Alice Bob Carly );

My @ names_2 = qw (David Edward Foo );

My $ names = natatime (2, @ names_1, @ names_2); # natatim (N at a time: Processing N groups at the same time) If there are multiple lists, just put them in it.

While (my @ name = $ names-> () {# traverse to facilitate subsequent output

Print "Got @ name \ n ";

}

# Output result

Got Alice Bob

Got Carly David

Got Edward Foo

[Example 08] Merge multiple lists into one list

The Code is as follows:

Use List: MoreUtils qw (mesh );

My @ array_1 = 'A'... 'D ';

My @ array_2 = 1 .. 4;

My @ array_3 = qw (jerry alice wendy );

My @ array_new = mesh (@ array_1, @ array_2, @ array_3 );

Print @ array_new;

# Output result:

A => 1 => jerry => B => 2 => alice => C => 3 => wendy => D => 4

1st times: Get A from the first list, 1 from the second list, and jerry from the third list.

2nd times: B is taken from the first list, 2 is taken from the second list, and alice is taken from the third list.

......

And so on!

[Example 09] adding characters to a specified string

You can use the insert_after subroutine in List: MoreUtils.

The Code is as follows:

Use v5.10;

Use List: MoreUtils qw (: all );

My @ list = qw/This is a list /;

Insert_after {$ _ eq 'A'} "longer" => @ list;

Print @ list; # This is a longer list

[Example 10] perform operations on the two lists. After the first list is square, sum the values with the second list.

You can use the pairwise subroutine in List: MoreUtils.

The Code is as follows:

Use v5.10;

Use List: MoreUtils qw (: all );

@ M = (1 .. 4 );

@ N = (100 .. 103 );

@ X = pairwise {($ a * $ a) + $ B} @ m, @ n; #101 105 111 119

[Summary]

If the above method is implemented using a common method, it requires loop traversal for multiple times, which is very troublesome, but complicated algorithms and data structures are implemented and encapsulated in the List module using C, then we can implement many seemingly complex functions. We can also see that the power of Perl lies in CPAN, which is supported by many modules!

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.