Perl List::util Module Usage examples

Source: Internet
Author: User
Tags foreach min shuffle


This article mainly introduces the Perl List::util module uses the example, this article gives the scanning matches the condition the list and takes out the first to satisfy the condition, asks for 1 to 1000 and, asks for a group of numbers maximum and the minimum value and so on the practical function code, needs the friend may refer to the next



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:



The code is as follows:



Use 5.010;



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



foreach (@names) {



if (/bwendyb/i) {



$match = $_;



Last



}



}



Say $match;



If you use the List::util module to provide a single subroutine, it will be much simpler.



The code is as follows:



Use List::util QW (a);



My $match = @names/bwendyb/i}; #Found the first Wendy to terminate

If you change numbers, for example, ask for

"Example 02" for the and between 1 to 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 simple:

The code is as follows:

Use List::util qw (sum);

My $total = SUM (1..1000); #Result 500500

"Example 03" asks for the maximum and minimum values for a set 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, temporarily as the maximum value.

Foreach (@_) {#traversal array @_

If ($_> $max _so_far) {#See if other elements have a larger than $max _so_far.

$max _so_far = $_;} #if there is, update the maximum value variable

}

$max _so_far;

}

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

Print $_maxdata; #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; #Result is 10

In the same way, using the Min subroutine provided by the List::util module, you can find the minimum value:

The code is as follows:

Use List::util qw (min);

My $min = Min (2, 3, 8, 5, 10); #min is 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:

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 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!

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 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:

The code is as follows:

Use List::moreutils QW (none of all);

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

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

Elsif (Any {$_ >} @numbers) {print ' Some elements over 50n ';}

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

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

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 simultaneous) there are multiple lists. Just put it in there.

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

Print "Got @namen";

}

#output result

Got Alice Bob

Got Carly David

Got Edward Foo

"Example 08" merges 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

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

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 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

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 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.