Perl list:: util module usage instance

Time:2021-12-2

In Perl, there are some modules dedicated to processing list data, such as list:: util module, which is included in the standard library and can provide various efficient common list processing tools. Because it is implemented in C language, the speed is generally very fast!

[example 01] scan a list that meets the conditions and take out the first one that meets the conditions
General practice:

Copy codeThe 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 will be much simpler

Copy codeThe 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 replace it with numbers, such as requirements


[example 02] find the sum between 1 and 1000
General practice:

Copy codeThe 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 instead, it is also very simple:

Copy codeThe code is as follows:

use List::Util qw(sum); 
my $total = sum(1..1000); # Result 500500  


[example 03] find the maximum and minimum values of a group of numbers
General practice:

Copy codeThe code is as follows:

#! /usr/bin/perl;   
use utf8;   
   
sub max {   
   my($max_so_far) = shift @_; # The first value in the array is temporarily regarded as the maximum value.    
   foreach(@_){                 # Traversal [email protected]_    
   if($_>$max_so_far){          # See if 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 instead, it is very simple:

Copy codeThe 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 find the minimum value:

Copy codeThe 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 the conventional method is used, it must be compared one by one in order. It can be easily realized by using the maxstr subroutine of list:: util:

Copy codeThe 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] randomly sort the elements in the list
If you use the conventional method, it is difficult to implement, but if you use the shuffle subroutine in list:: util, it is very simple. You can do it with one command!

Copy codeThe 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 there is no element in the list, or any element, or all elements meet the conditions. Support grep like syntax
It is difficult to implement with conventional methods, but it is also easy to implement with list:: moreutils. The code is as follows:

Copy codeThe 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 self-contained module and needs to be downloaded


[example 07] process multiple name lists at the same time, taking out 2 digits each time

Copy codeThe 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: process n groups at the same time) has multiple lists. Just put them in it  
while (my @name = $names->()) {                  # Traversal, convenient for later output  
    print “Got @name\n”; 

#Output results  
Got Alice Bob 
Got Carly David 
Got Edward Foo 

 

[example 08] merge multiple lists into one list

Copy codeThe 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 results:  
A=>1=>jerry=> B=>2=>alice=> C=>3=>wendy=> D=>4

 

The first time: take a from the first list, 1 from the second list and Jerry from the third list
The second time: take B from the first list, 2 from the second list, and Alice from the third list
……
And so on!
[example 09] add characters to the specified string
You can use insert in list:: moreutils_ After subroutine

Copy codeThe 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] operate two lists. After the first list is squared, sum it with the second list
You can use the pairwise subroutine in list:: moreutils

Copy codeThe 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 an ordinary way, it requires multiple loop traversal, which is very troublesome. If the complex algorithm and data structure are implemented in C and encapsulated in the list module, many seemingly complex functions can be realized. It can also be seen that the strength of Perl lies in cpan, which is supported by many modules!