perl模块推荐12—Set::Scalar
摘要
Set::Scalar 是一个优秀的集合操作模块。交集,补集(差集),并交集等操作
安装
cpanm Set::Scalar
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
use strict;
use Set::Scalar;
my @num1=(1,2,2,3,3,3,4,5); my @num2=(3,4,,5,6,,7,7,8);
my $set1=Set::Scalar->new(@num1); print join "-",$set1->elements,"\n";
my $set2=Set::Scalar->new(@num2);
my $inter_set= $set1 * $set2; print join "-",$inter_set->elements,"\n";
my $union_set=$set1+$set2; print join "-",$union_set->elements,"\n";
my $set1_diff=$set1-$set2; print join "-",$set1_diff->elements,"\n";
my $set_uniq=$set1 % $set2; print join "-",$set_uniq->elements,"\n";
|
方法操作
上面的符号重载和下面的方法是等效的
1 2 3 4
| $u = $s->union($t); # + $i = $s->intersection($t); # * $d = $s->difference($t); # - $e = $s->symmetric_difference($t); #%
|
集合中的元素具有互异性,无序性