在日常的文本处理中,有时候要输出唯一的行,这种工作有多种方法实现...

以下是一种:

my %hash;
while (<>) {
    chomp;
    if (defined $hash{$_}) {
        $hash{$_} = undef;    #重复?
        next;
    }
    $hash{$_} = [$_, $.];
}

my @uniq = grep defined, values %hash;         #过滤到重复的
@uniq = sort {$a->[1] <=> $b->[1]} @uniq;    #按保存的顺序排序
say $_->[0] for @uniq

 

这是来自CU 的一个代码,比较的简单,故贴在这里,供观赏。

 

还有一例:

my @x=(1,2,3,2,1,4);

my %hash;

$hash{$_}++ for @x;

for (@x) {

print if $hash{$_} < 2;

}