views:

169

answers:

3
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";

returns :

00A
1A

It should be 0A or not?!

Thank you

+9  A: 

I guess. Here's the 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: }

The filter in line 6 only applies to duplicate and true values, so duplicate "0" values are not caught. Why don't you submit a bug report?

mobrule
*Why don't you submit a bug report?* - The reason he's asking the question is because he do not know if this is a bug, or there's just something wrong in his code. :)
Ruel
@Ruel: I think mobrule wants to encourage the OP to learn about the RT system, rather than him filing a bug himself. It's quite an achievement to have found one's first bug in a CPAN module (and another to find one in core Perl).
Ether
Ok, "List::MoreUtils qw/ uniq /" seem works fine :)
+16  A: 

I would suggest using the uniq function from 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";

The Uniq module is at version 0.1, has had only one release, and that was back in 2003. Always check for the that sort of information when selecting a module. Modules that have multiple releases (especially recent releases) tend to be better than modules that have only one or a few releases.

Chas. Owens
I would recommend using List::AllUtils (http://search.cpan.org/dist/List-AllUtils/) instead of List::MoreUtils. The benefit of doing so is that you don't have to remember whether what you are looking for is in List::Util or List::MoreUtils.
mfollett
Please don't use version < 1 or any other numeric criteria as a reason not to use a module; There are plenty of fine 0.0x modules out there that people shouldn't be given an excuse to overlook. Multiple releases is a much better thing to check, and outstanding bug reports better yet.
ysth
@ysth 0.1 isn't really the problem, it is that fact that 0.1 was the first and only release. And that the release was back in 2003. Also the documentation is not up to normal CPAN standards. This is another sign that it is probably not a very good module.
Chas. Owens
@mfollett I was unaware of `List::AllUtils`. I will have to take a look.
Chas. Owens
+7  A: 

This appears to be the same bug reported on CPAN for module Array::Uniq: Array::Uniq doesn't handle arrays containing entries with zero values. The only difference between Uniq and Array::Uniq is the package name; I proved this by a unix diff of their .pm files. They were both created by the same author.

That bug report was submitted 4 years ago (2006), it is still open, and the author never replied to it. The author should eliminate one of these two redundant modules. I think it is reasonable to assume the author has stopped maintaining these two modules. Use one of the alternative modules proposed by the other Answers.

toolic