Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

String::Combination Package Bug?

by b4swine (Pilgrim)
on Jan 02, 2008 at 15:37 UTC ( [id://659992]=perlquestion: print w/replies, xml ) Need Help??

b4swine has asked for the wisdom of the Perl Monks concerning the following question:

I used the String::Combination package, downloaded from the ActivePerl site, and got what appear so plainly like a bug that I believe I must be wrong somewhere in my understanding. The code
use String::Combination qw(combination); my @comb = combination('12345678',8); print "@comb[6..12]\n@comb[62..68]\n";
produces the output
23456781 32456781 34256781 34526781 34562781 34567281 34567821 23456781 32456781 34256781 34526781 34562781 34567281 34567821
As far as I see it, there should not be any repetitions. Am I misunderstanding something, or is this a bug?

Replies are listed 'Best First'.
Re: String::Combination Package Bug?
by amarquis (Curate) on Jan 02, 2008 at 16:46 UTC

    I downloaded String::Combination via ActiveState's PPM, and produced this behavior.

    I couldn't find the distro (or the author) on CPAN, but the documentation in the distro's POD seems to indicate that the combinations should be unique:

    "From a set of n elements, given an integer k, the algorithm calculates k! * n!/(k!(n-k)!) elements that are all the combinations of k elements from the set of n elements. "

    I believe Active's PPM just grabs "whatever will build" from CPAN, and doesn't do any other testing. Perhaps the C library that this module is the XS glue for is making assumptions about the machine it is running on?

    Edit: Forgot to mention, you can grab Algorithm::Combinatorics or Math::Combinatorics via PPM. Looking quickly, I'm thinking that they are overkill for your problem, but they are an option at least.

Re: String::Combination Package Bug?
by gamache (Friar) on Jan 02, 2008 at 16:38 UTC
    I noticed a few strange things about String::Combination...
    • It's not on CPAN, despite having been uploaded in 2004
    • It's remained at 0.01 since then
    • Google is largely unaware of its presence
Re: String::Combination Package Bug?
by shmem (Chancellor) on Jan 02, 2008 at 17:21 UTC
    Looks like a bug. combination($str,$k) returns each combination ($k-2)! times.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: String::Combination Package Bug?
by Khen1950fx (Canon) on Jan 02, 2008 at 23:35 UTC
    String::Combination worked for me. Try this:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use String::Combination qw(combination); my @comp = combination('12345678', 8); print "@comp\n";
    It works with k integers 2 thru 8.
Re: String::Combination Package Bug?
by Anonymous Monk on Jan 03, 2008 at 08:27 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://659992]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 21:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found