Perl uses grep map to calculate the instance code of intersection, union, and supplement

Source: Internet
Author: User

CopyCodeThe Code is as follows :#! /Usr/bin/perl
#
# Use grep map to obtain the Intersection Set and supplement set of the two lists
#
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} @;
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; # intersection
Print "intersection: @ inter \ n ";
# @ A, @ B Union
My % merge = map {$ _ => 1} @ A, @ B; # Calculate the Union
My @ merge = keys (% merge );
Print "union: @ merge \ n ";
# @ A, @ B's complement set @ ca, @ CB, that is, the complement set of @ A and @ B relative to @ merge
My @ CA = grep {! $ A {$ _ }}@ merge;
My @ cb = grep {! $ B {$ _ }}@ merge;
Print "\ @ A's supplement: @ Ca \ n ";
Print "\ @ B's supplement: @ 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.