perl用grep map求交集、並集、補集的執行個體代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:#!/usr/bin/perl
#
# 用grep map 擷取兩個列表的交集並集、補集
#
use strict;
my @a=("a","b","c","d","e");
my @b=("b","g","f","e");
print "列表a資料: @a \n";
print "列表b資料: @b \n";
my %a = map{$_ => 1} @a;
my %b = map{$_ => 1} @b;
my @c = map{$_ => 1} @a; # => 操作符起到了hash賦值的作用
# @a @b 交集
my @inter = grep {$a{$_}} @b; # 求交集
print "交集:@inter \n";
# @a,@b 並集
my %merge = map {$_ => 1} @a,@b; # 求並集
my @merge = keys (%merge);
print "並集:@merge \n";
# @a,@b的補集@ca,@cb,即@a和@b相對於@merge的補集
my @ca = grep {!$a{$_}} @merge;
my @cb = grep {!$b{$_}} @merge;
print "\@a的補集:@ca \n";
print "\@b的補集:@cb \n";
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.