7 Perl Arrays advanced Operation tips Sharing _perl

Source: Internet
Author: User

1. Remove duplicate elements from an array:

Using the GREP function code fragment:
Code:

Copy Code code as follows:

My @array = (' A ', ' B ', ' C ', ' a ', ' d ', 1, 2, 5, 1, 5);
My%count;
My @uniq_times = grep {+ + $count {$_} < 2;} @array;

Using the Transform hash code fragment:
Code:

Copy Code code 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 array:

Copy Code code as follows:

Push @array1, @array2;

3, quickly find the maximum value, do not know the program apes, this way:

Copy Code code as follows:

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

Know how to do this:
Copy Code code as follows:

Use List::util QW (max);
My $max _num = max (0.. 1000);

You know, they're still doing this:
Copy Code code as follows:

Use List::util QW (MAXSTR);
My $max _str = Maxstr (QW (Fido Spot Rover));

String comparisons are played in the palm. There is also sum:
Copy Code code as follows:

Use List::util qw (sum);
My $sum = SUM (1.. 1000);

4. List Merge

Numeric summation, you can also use reduce in List::util:

Copy Code code as follows:

Use List::util QW (reduce);
My $sum = reduce {$a + $b} 1. 1000;

Like sort, reduce also uses the code block as an argument, but the mechanism is slightly different. Each iteration, the first two elements are taken out of the argument list, respectively, to alias $a and $b, so that the length of the argument list is shortened to two elements. Reduce then presses the calculated result returned by the statement block back to the head of the parameter list. So reciprocating, until the final list of only one element, that is, the calculation results of the iteration $sum.

Okay, here's the thing:

Copy Code code as follows:

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

5, to determine whether there are elements to match

Simply using Perl to find the first element in the list that meets a certain condition is a bit more troublesome than finding all the eligible. The following example determines whether there are more than 1000 elements:

Copy Code code as follows:

My $found _a_match = grep {$_ > 1000} @list;

Note: If the @list has 100 million elements, what is the 1001? grep still loops 100 million times, of course you can control it below yourself:
Copy Code code as follows:

My $found _a_match = 0;
foreach my $elem (@list) {
$found _a_match = $elem if $elem > 1000;
Last if $found _a_match;
}

Or that sentence, not simple ~~~list::util have ready-made things:
Copy Code code as follows:

Use List::util QW (a);
My $found _a_match = fist {$_ > 1000} @list;

In the List::moreutils module, there are a number of utility functions available:
Copy Code code as follows:

My $found _a_match = no {$_ > 1000} @list;
My $all _greater = all {$_ > 1000} @list;
My $none _greater = none {$_ > 1000} @list;
My $all _greater = notall {$_% 2} @list;

6. Traverse multiple lists at one time

In general, when we traverse multiple business-related lists at the same time, we often use array subscript traversal:

Copy Code code 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 this, what's your feeling?
Copy Code code as follows:

Use List::moreutils QW (pairwise);
My @c = pairwise {$a + $b} @a, @b;

Pairwise is only suitable for two-list synchronous computations and three each_array:
Copy Code code as follows:

Use List::moreutils QW (Each_array);

My $ea = Each_array (@a, @b, @c);

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


It's still a little annoying, but it's okay.

7. Array merging

Merging multiple arrays Of course you can write your own, but it's not as convenient as moreutils mesh:

Copy Code code 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 ...

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.