2013年3月7日木曜日

複数配列から重複しない行のみ抽出する

複数配列から重複しない行のみ抽出するには、以下のようにgrepを使います。



#!/opt/local/bin/perl
use utf8;


my @foo = qw(ari hituji inko cat dog);
my @baa = qw(cat ebi kuma hituji tora cat);

my %count;
$count{$_}++ for (@foo, @baa); # @fooと@baaの要素の数を数える
@hoge = grep { $count{$_} < 2 } keys %count; # 要素数が2より小さいもののみ抽出

my $i = 0;
foreach my $item(@hoge) {
    print "[$i]:$item\n";
    $i++;
}





結果は以下のようになります。



[0]:ari
[1]:inko
[2]:dog
[3]:ebi
[4]:kuma
[5]:tora




0 件のコメント:

コメントを投稿