Perl uses grep map to find the intersection, set, and complement instance code _perl

Source: Internet
Author: User
Copy Code code as follows:

#!/usr/bin/perl
#
# using grep map to get the intersection and set of two lists, the complement
#
Use strict;
My @a= ("a", "B", "C", "D", "E");
My @b= ("B", "G", "F", "E");
Print "List a data: @a \ n";
print "List B data: @b \ n";
My%a = map{$_ => 1} @a;
My%b = map{$_ => 1} @b;
My @c = map{$_ => 1} @a; # The => operator plays the role of hash assignment
# @a @b Intersection
My @inter = grep {$a {$_}} @b; # Find the Intersection
Print "intersection: @inter \ n";
# @a,@b and set
My%merge = map {$_ => 1} @a,@b; # Seek and set
My @merge = keys (%merge);
Print "and set: @merge \ n";
# @a,@b's complement @ca, @cb, that is @a and @b, relative to @merge's complement
My @ca = grep {! $a {$_}}} @merge;
My @cb = grep {! $b {$_}}} @merge;
Print "\@a's complement: @ca \ n";
Print "\@b's complement: @cb \ n";
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.