Subject: | bug and suggested fix |
Date: | Mon, 16 Mar 2015 19:18:29 -0700 |
To: | bug-Statistics-TopK [...] rt.cpan.org |
From: | Pierre Demartines <pierre [...] demartines.com> |
Hello,
I was trying to use your implementation of TopK, and was getting an error.
Can't use an undefined value as a HASH reference at
/usr/lib/perl5/site_perl/5.8.8/Statistics/TopK.pm line 79.
Here is a trivial test that passes:
perl -e 'use Statistics::TopK; $t = Statistics::TopK->new(5); for (qw/a a b
c a c/) { $t->add($_) } print join(" ", $t->top)."\n"'
c a b
...and one that fails:
perl -e 'use Statistics::TopK; $t = Statistics::TopK->new(5); for (qw/a a b
c a c/) { $t->add($_) } print join(" ", $t->counts)."\n"'
Can't use an undefined value as a HASH reference at
/usr/lib/perl5/site_perl/5.8.8/Statistics/TopK.pm line 79.
Now, here is the proposed (minor) fix:
diff --unified orig-TopK.pm fix-TopK.pm
--- orig-TopK.pm 2015-03-16 18:57:04.000000000 -0700
+++ fixd-TopK.pm 2015-03-16 19:01:49.000000000 -0700
@@ -72,11 +72,13 @@
}
sub top {
- return keys %{$_[0]->[_COUNTS]};
+ my $self = shift;
+ return keys %{$self->[_COUNTS]};
}
sub counts {
- return %{$_->[0]->[_COUNTS]};
+ my $self = shift;
+ return %{$self->[_COUNTS]};
}
The test that was failing now passes:
perl -e 'use Statistics::TopK; $t = Statistics::TopK->new(5); for (qw/a a b
c a c/) { $t->add($_) } print join(" ", $t->counts)."\n"'
c 2 a 3 b 1
Best,
~Pierre
PS: config:
Distribution G/GR/GRAY/Statistics-TopK-0.01.tar.gz
Module Statistics::TopK (G/GR/GRAY/Statistics-TopK-0.01.tar.gz)
This is perl, v5.8.8 built for x86_64-linux-thread-multi
Linux gp 2.6.18-398.el5 #1 SMP Tue Sep 16 20:50:52 EDT 2014 x86_64 x86_64
x86_64 GNU/Linux