Perl List::util Module Use instance _perl

Source: Internet
Author: User
Tags shuffle

In Perl, there are modules dedicated to processing list data, such as the List::util module, which is included in the standard library and provides a variety of efficient common list processing tools. Because of its use of C language to achieve, speed is generally very fast!

Example 01 scans a list that meets the criteria and takes out the first qualifying
General Practice:

Copy Code code 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 List::util module to provide a single subroutine, it will be much simpler.
Copy Code code as follows:

Use List::util QW (a);
My $match = @names/\bwendy\b/i}; #找到第一个Wendy即终止
If you change numbers, for example, ask for

"Example 02" for the and between 1 to 1000
General Practice:
Copy Code code as follows:

Use 5.010;
my $total = 0;
foreach (1..1000) {
$total + + $_;
}
Say $total; #结果500500

If you use the Sum subroutine provided by the List::util module, it is also simple:
Copy Code code as follows:

Use List::util qw (sum);
My $total = SUM (1..1000); #结果500500

"Example 03" asks for the maximum and minimum values for a set of numbers.
General Practice:
Copy Code code as follows:

#! /usr/bin/perl;
Use UTF8;

Sub Max {
My ($max _so_far) = Shift @_; #数组中第一个值, temporarily as the maximum value.
foreach (@_) {#遍历数组 @_
if ($_> $max _so_far) {#看其它元素是否有比 large value $max _so_far.
$max _so_far = $_;} #如果有话, updating the maximum value variable
}
$max _so_far;
}

My $_maxdata = &max (2,3,8,5,10);
Print $_maxdata; #结果为10

If you use the Max subroutine provided by the List::util module, it is very simple:
Copy Code code as follows:

Use List::util QW (max);
My $max = Max (2, 3, 8, 5, 10);
Print $max; #结果为10

In the same way, using the Min subroutine provided by the List::util module, you can find the minimum value:
Copy Code code as follows:

Use List::util qw (min);
My $min = Min (2, 3, 8, 5, 10); #最小值为2

"Example 04" to sort a set of strings
If you use conventional methods, you must compare them sequentially, using the List::util Maxstr subroutine can be easily achieved:
Copy Code code as follows:

Use List::util QW (MAXSTR);
My $max _str = Maxstr (qw/jerry Betty Alice Fred Barney jerry/);
Print $max _str;

"Example 05" random ordering of elements in a list
If you use the conventional method, it is difficult to achieve, and the list::util in the Shuffle subroutine, it is very simple, a command to fix!
Copy Code code 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 checks that the list does not have an element, or that there are any elements, or that all elements are eligible. Support for similar grep syntax
If you use conventional methods, it is very difficult to implement, but also easy to implement with List::moreutils, the code is as follows:
Copy Code code as follows:

Use List::moreutils QW (none of all);
My @numbers = QW (7 4 1 3 78);
if (none {$_ >} @numbers) {print ' No elements over 100\n ';}
elsif (Any {$_ >} @numbers) {print ' Some elements over 50\n ';}
elsif (All {$_ <10} @numbers) {print ' All elements < 10\n ';}
Note:list::moreutils modules are not self-contained and need to be downloaded.

"Example 07" handles multiple list of names at the same time, 2 bits at a time
Copy Code code 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 simultaneously) there are multiple lists. Just put it in there.
while (my @name = $names-> ()) {#遍历 to facilitate subsequent output
print "Got @name \ n";
}
#输出结果
Got Alice Bob
Got Carly David
Got Edward Foo

"Example 08" merges multiple lists into one list

Copy Code code 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;
#输出结果:
a=>1=>jerry=> b=>2=>alice=> c=>3=>wendy=> d=>4

1th time: Take a in the first list, remove 1 from the second list, and remove Jerry from the third list.
2nd time: Take B in the first list, remove 2 from the second list, and remove Alice from the third list
......
The analogy!
"Example 09" adds a character to the specified string
You can use the Insert_after subroutine in List::moreutils

Copy Code code 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 operates on two lists, after the first list is squared, and the second list is summed
You can use the Pairwise subroutine in List::moreutils
Copy Code code 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 in a normal way, need multiple loops traversal, very troublesome, and the complex algorithm, data structure in C to implement and encapsulated in the list module, you can achieve a lot of seemingly complex functions, which can also be seen in Perl's powerful lies in Cpan, there are many modules to support!

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.