SoFunction
Updated on 2025-04-07

Share 7 advanced operation skills of perl arrays

1. Remove duplicate elements from an array:

Using grep function code snippet:
Code:

Copy the codeThe code is as follows:

my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
my %count;
my @uniq_times = grep { ++$count{ $_ } < 2; } @array;

Use the conversion hash code snippet:
Code:

Copy the codeThe code is as follows:

my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
my %saw;
@saw{ @array } = ( );
my @uniq_array = sort keys %saw;

2. Merge two arrays:

Copy the codeThe code is as follows:

push @array1, @array2;

3. Quickly find the maximum value. Don’t know the programmer, do this:

Copy the codeThe code is as follows:

my @nums = 0 .. 1000;
my $max = $nums[0];
foreach (@nums) {
$max = $_ if $_ > $max;
}

What you know is doing this:
Copy the codeThe code is as follows:

use List::Util qw(max);
my $max_num = max( 0 .. 1000 );

They still do this if they know:
Copy the codeThe code is as follows:

use List::Util qw(maxstr);
my $max_str = maxstr ( qw( Fido Spot Rover ) );

Strings are more fun in your palm. And sum:
Copy the codeThe code is as follows:

use List::Util qw(sum);
my $sum = sum ( 1 .. 1000 );

4. List merger

To sum numbers, you can also use the reduce in List::Util:

Copy the codeThe code is as follows:

use List::Util qw(reduce);
my $sum = reduce { $a + $b } 1 .. 1000;

Similar to sort, reduce also uses code block as parameter, but the operating mechanism is slightly different. In each iteration, first take out the first two elements from the parameter list and set them to the alias $a and $b respectively, so that the length of the parameter list will be shortened to two elements. Then reduce the calculation result returned by the statement block back to the head of the parameter list. This goes on and on until the end, there is only one element left in the list, that is, the iterative calculation result $sum.

OK, this is OK:

Copy the codeThe code is as follows:

my $product = reduce { $a * $b } 1 .. 1000;

5. Determine whether there is an element matching

It is purely implemented with Perl, and finding the first element in the list that meets a certain condition is more troublesome than finding all that meet the conditions. In the following example, determine whether there are elements greater than 1000:

Copy the codeThe code is as follows:

my $found_a_match = grep { $_ > 1000 } @list;

Note: If @list has 100 million elements, and what you are looking for is 1001? grep will still cycle 100 million times, of course you can control it yourself:
Copy the codeThe code is as follows:

my $found_a_match = 0;
foreach my $elem (@list) {
$found_a_match = $elem if $elem > 1000;
last if $found_a_match;
}

The same sentence is not simple~~~List::Util has something ready-made:
Copy the codeThe code is as follows:

use List::Util qw(first);
my $found_a_match = fist { $_ > 1000 } @list;

In the List::MoreUtils module, there are also many practical functions:
Copy the codeThe code is as follows:

my $found_a_match = any { $_ > 1000 } @list;
my $all_greater = all { $_ > 1000 } @list;
my $none_greater = none { $_ > 1000 } @list;
my $all_greater = notall { $_ % 2 } @list;

6. Iterate through multiple lists at once

Generally, when we traverse multiple business-related lists at the same time, we often use array subscripts to traverse:

Copy the codeThe code is as follows:

my @a = ( ... );
my @b = ( ... );
my @c;

foreach my $i ( 0 .. $#list ) {
my ( $a, $b ) = ( $a[$i], $b[$i] );
push @c, $a + $b;
}


Look at the following one, what do you feel?
Copy the codeThe code is as follows:

use List::MoreUtils qw(pairwise);
my @c = pairwise { $a + $b } @a, @b;

pairwise is only suitable for synchronous calculations of two lists, and then use each_array after three:
Copy the codeThe code is as follows:

use List::MoreUtils qw(each_array);

my $ea = each_array( @a, @b, @c );

my @d;
while ( my ( $a, $b, $c ) = $ea->() ) {
push @d, $a+$b+$c;
}


Although it's still a little annoying, it's okay.

7. Combination and

Of course you can write the operation of merging multiple arrays yourself, but it is not as convenient as MoreUtils' mesh:

Copy the codeThe code is as follows:

use List::MoreUtils qw(mesh);

my @odds = qw/ 1 3 5 7 9/;
my @evens= qw/ 2 4 6 8 0/;

my @nums = mesh @odds, @evens; # print: 1 2 3 4 ...