Subject: | Bug in cpan module - uniq |
Date: | Mon, 16 Apr 2007 09:11:42 -0700 |
To: | <bug-uniq [...] rt.cpan.org> |
From: | "Green, Damian" <damian.k.green [...] medtronic.com> |
Hi, I found and downloaded the following module:
http://search.cpan.org/~syamal/Uniq-0.01/Uniq.pm
And found that the "distinct" subroutine has a bug. I've Identified the
problem, but don't have the experience to update the module on the CPAN
site...
The source:
sub distinct{
# Eliminates values mentioned more than once from a list of
# sorted values presented.
my $prev = undef;
my $ctr = 0;
my @out;
foreach my $val (@_){
if ($prev){
if ($prev eq $val){
$ctr ++;
next;
}
push(@out,$prev) if ($ctr == 1);
$prev = $val;
$ctr = 1;
next;
}else{
$prev = $val;
$ctr = 1;
}
}
return @out;
}
====================
The problem is that the last element in the list is not getting pushed
on to the list being returned out. A simple fix would be to add the
following right before the return @out; (outside the foreach block)...
if ($prev && $ctr == 1){ #<---takes care of last element
in the list
push(@out,$prev);
}
Damian Green