use Uniq;
my @test1 = ("0","0","A");
my @test2 = ("1","1","A");
@test1 = uniq sort @test1;
@test2 = uniq sort @test2;
print "$_" for @test1;
print "\n";
print "$_" for @test2;
print "\n";
返回:
00A
1A
它应该是0A?
谢谢
答案 0 :(得分:16)
我建议使用uniq
中的List::MoreUtils
函数:
use strict;
use warnings;
use List::MoreUtils qw/uniq/;
my @test1 = uniq qw(0 0 A);
my @test2 = uniq qw(1 1 A);
print "@test1\n@test2\n";
Uniq
模块的版本为0.1,只有一个版本,而且是在2003年。在选择模块时,请务必检查这类信息。具有多个版本(特别是最新版本)的模块往往比只有一个或几个版本的模块更好。
答案 1 :(得分:9)
我猜。这是source code to uniq
1: sub uniq{
2: # Eliminates redundant values from sorted list of values input.
3: my $prev = undef;
4: my @out;
5: foreach my $val (@_){
6: next if $prev && ($prev eq $val);
7: $prev = $val;
8: push(@out, $val);
9: }
10: return @out;
11: }
第6行中的过滤器仅适用于重复和 true 值,因此不会捕获重复的"0"
值。你为什么不submit a bug report?
答案 2 :(得分:7)
这似乎与模块Array::Uniq
的CPAN上报告的错误相同:Array::Uniq doesn't handle arrays containing entries with zero values。 Uniq和Array::Uniq之间的唯一区别是包名称;我通过他们的.pm文件的unix diff
证明了这一点。它们都是由同一作者创建的。
该错误报告是在4年前(2006年)提交的,它仍然是公开的,作者从未回复过它。作者应该消除这两个冗余模块中的一个。我认为假设作者已停止维护这两个模块是合理的。使用其他答案提出的替代模块之一。