how to clear the cache inside the module use Math::Combinatorics;
am 06.06.2011 06:06:34 von eventual
--0-1563447251-1307333194=:27945
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi,=0ALooking at the combination script below, what must I do so that the o=
utput of Round 1, Round=A02 and Round=A03 are identical. =0AThanks.
=
=0A##### script below ###########
#!/usr/bin/perl=0Ause strict;=
=0Ause warnings;=0Ause Math::Combinatorics;
##########################=
##############################=0A{=0Amy $ctr =3D 0;=0Asub get_numbers {=0A=
=A0 $ctr++;
my @combination_datas =3D qw(1 2 3 4 5 6 7);=
my $combination_length =3D 6;
print "Round $=
ctr) \@combination_datas =3D @combination_datas\n";
my $c=
ombinat =3D Math::Combinatorics->new(count =3D> $combination_length,
=
=A0=
=A0 data =3D> [@combination=
_datas],
=A0= A0 =
=A0 );
=A0=
=A0 while(my @combo =3D $combinat->next_combination){
pri=
nt "@combo\n";
}=0A} #end sub=0A}#end=0A#######################=
#############################=0Amy $ctr =3D 0;=0Awhile ($ctr < 3){
=A0=
=A0 $ctr++;
get_numbers;=0A}=0Aprint "done\n";
--0-1563447251-1307333194=:27945--
Re: how to clear the cache inside the module use Math::Combinatorics;
am 09.06.2011 00:48:01 von Rob
On 06/06/2011 05:06, eventual wrote:
> Hi,
> Looking at the combination script below, what must I do so that the output of Round 1, Round 2 and Round 3 are identical.
> Thanks.
>
> ##### script below ###########
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Math::Combinatorics;
>
> ########################################################
> {
> my $ctr = 0;
> sub get_numbers {
> $ctr++;
> my @combination_datas = qw(1 2 3 4 5 6 7);
> my $combination_length = 6;
>
> print "Round $ctr) \@combination_datas = @combination_datas\n";
>
> my $combinat = Math::Combinatorics->new(count => $combination_length,
> data => [@combination_datas],
> );
> while(my @combo = $combinat->next_combination){
> print "@combo\n";
> }
> } #end sub
> }#end
> ####################################################
> my $ctr = 0;
> while ($ctr< 3){
> $ctr++;
> get_numbers;
> }
> print "done\n";
This problem is a fundamental bug in Math::Combinatorics. It relies on
the data being sortable, and by default sorts by doing a string
comparison on references to the data items. Doing things this way means
that the results cannot be consistent, as data with equal values will
rarely have the same reference.
It is possible to provide a sort comparison subroutine to the
constructor using the 'compare' option, which alleviates this bug but
causes a different one because the custom comparison is not always
called in the right context.
The author does not list this in the known bugs, but writes this comment
in the code:
> #OK, this is hokey, but I don't have time to fix it properly right now.
> #We want to allow both user-specified sorting as well as our own
> #reference-based internal sorting -- the latter only because unit tests
> #are failing if we don't have it. Additionally, we don't want to require
> #the triple derefernce necessary for comparison of the pristine data in
> #the user-supplied compare coderef. The solution for now is to do an
> #if/else. If you're staring at this please fix it!
(This is on a module data December 2006).
Math::Combinatorics is also very poorly implemented, and your solution
should be to use Algorithm::Combinatorics instead. The only change is to
your 'use' statement and to the get_numbers subroutine, which should
read as below. (There are other issues with your code, but this will get
it working.)
HTH,
Rob
use Algorithm::Combinatorics qw/combinations/;
sub get_numbers {
$ctr++;
my @combination_datas = qw(1 2 3 4 5 6 7);
my $combination_length = 6;
print "Round $ctr) \@combination_datas = @combination_datas\n";
my $combinat = combinations([@combination_datas], $combination_length);
while(my $combo = $combinat->next) {
print "@$combo\n";
}
}
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/