List module for columns in Perl, and list module for perl tools

Source: Internet
Author: User

List module for columns in Perl, and list module for perl tools

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:

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 List: Util module to provide FirstSubprograms are much simpler.
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:

Use 5.010; my $ total = 0; foreach (1 .. 1000) {$ total + =$ _;} say $ total; # result 500500
If you use the List: Util module to provide SumThe subroutine is also simple:
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:

#! /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 $ max_so_far;} my $ _ MaxData = & max (2, 3, 8, 5, 10); print $; # The result is 10.

If you use the List: Util module to provideMaxSubroutine, which is very simple:

Use List: Util qw (max); my $ max = max (2, 3, 8, 5, 10); print $ max; # The result is 10.

Similarly, the List: Provided by the Util ModuleMinSubroutine, minimum value:

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 order, and use the List: UtilMaxstrSubprograms can be easily implemented:

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 the conventional method, it is difficult to implement it, but use the List: UtilShuffleSubroutine, it's very simple, just a command!

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:

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

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: Processes N groups at the same time) if you have multiple lists, you can just put them in the while (my @ name = $ names-> () {# traversal to facilitate print "Got @ name \ n ";} # output result Got Alice BobGot Carly David Got Edward Foo
[Example 08] Merge multiple lists into one list
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.

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.

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.