1.
#!/usr/bin/perl
use strict;
my %hash;
my @array = (1..10,5,20,2,3,4,5,5);
#grep 保存符合条件的元素
@array = grep { ++$hash{$_} < 2 } @array;
print join(" ",@array);
print "\n";
2.
#!/usr/bin/perl -w
use strict;
my %hash;
my @signer=`jarsigner -verify -verbose -certs Weibo.apk`;
@signer=grep {/X\.509/} @signer;
@signer=grep { ++$hash{$_} < 2 } @signer;
print "@signer";
#print join("\n",@signer);
#print "\n";
foreach my $a(@signer) {
my @b=split(/,/,$a);
print "$b[1]";
}
3.在数组中寻找唯一元素
#!/usr/bin/perl -w
use strict;
my @a=qw(a d f g h k a m l g);
my %hash=();
foreach (@a) {
$hash{$_}=1;
}
my @good=sort keys %hash;
print "value: @good\n";
4.寻找两个数组之间的交汇部分
#!/usr/bin/perl -w
use strict;
my @a=qw(a d f g h k m l g);
my @b=qw(l m d g k w z);
my %seen=();
foreach (@a) {
$seen{$_}=1;
}
my @c=sort grep($seen{$_},@b);
print "@c\n";
5.寻找两个数组之间不同部分
#!/usr/bin/perl -w
use strict;
my @a=qw(a d f g h k m l g);
my @b=qw(l m d g k w z);
my %seen=();
foreach (@a) {
$seen{$_}=1;
}
my @c=sort grep(!$seen{$_},@b);
print "@c\n";
阅读(2334) | 评论(0) | 转发(0) |