Skip Menu |

This queue is for tickets about the Uniq CPAN distribution.

Report information
The Basics
Id: 26406
Status: open
Priority: 0/
Queue: Uniq

People
Owner: Nobody in particular
Requestors: damian.k.green [...] medtronic.com
Cc: PLICEASE [...] cpan.org
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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
On Mon Apr 16 12:12:54 2007, damian.k.green@medtronic.com wrote: Show quoted text
> 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 >
I propose the attached diff that will fix this bug, and add a test for it. I also have forked a version of Uniq here that has to proposed fix: https://github.com/plicease/Uniq Syamala, if you want help in making a release to fix this issue, please let me know.
Subject: patch.diff
diff --git a/Changes b/Changes index 4b457d7..3854b0d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension Uniq. + - Fixed bug where last element passed to distinct was not considered distinct (rt26406) + 0.01 Thu Aug 21 15:56:43 2003 - original version diff --git a/Uniq.pm b/Uniq.pm index cebceff..b629046 100644 --- a/Uniq.pm +++ b/Uniq.pm @@ -62,6 +62,9 @@ sub distinct{ } } + if ($prev && $ctr == 1){ + push(@out,$prev); + } return @out; } 1; diff --git a/t/1.t b/t/1.t index b5579c8..cb12ca2 100644 --- a/t/1.t +++ b/t/1.t @@ -1,7 +1,7 @@ # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl 1.t' -print "1..6\n"; +print "1..7\n"; my $i = 1; @@ -35,3 +35,6 @@ printf "ok %d\n", $i++; print "not " if ( "@out" ne "A2 A7" ); printf "ok %d\n", $i++; +@ out = distinct sort "A1", "A2", "A3"; +print "not " if ( "@out" ne "A1 A2 A3" ); +printf "ok %d\n", $i++;