想几个方法,可以对ARRAY进行一下操作:
1. 2个array共同的部分
$common=xxx(\@array1,\@array2)
2. 2个array不同的部分
($part1,$part2)=xxx(\@array1,\@array2)
3. 去除array中相同的元素
@array_unique=xxx(@array)
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Data::Dumper;
- my @a = qw(a b c d e f);
- my @b = qw(v d c a f n);
- my %m;
- $m{$_}++ for @a,@b;
- my (@same,@diff,@union);
- while (my($k,$v) = each %m) {
- push @union,$k;
- push @same, $k if $v > 1;
- push @diff, $k if $v == 1;
- }
- print Dumper \@union;
- print Dumper \@same;
- print Dumper \@diff;
阅读(2592) | 评论(0) | 转发(0) |