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

reply to first problem-works-thank you. next problem with program is repeat permutations,sometimes as many as 5 in a row. What should be a routine 420 permutations from: aabbbcd- ends up over 700. need some code to add to prevent repetition. Please tell me exactly what code and exactly where to insert it in the program. thanks for your help!! email if you'd like swanhits@hotmail.com

Replies are listed 'Best First'.
Re: permute n elements2
by chromatic (Archbishop) on Oct 22, 2000 at 23:08 UTC
    Store the results in a hash. The values don't matter, just use the unique property of hash keys to your advantage.
    my %results; @results{permute(\@possibilities)} = 1;
      thanks for info. just need to know where to insert into the program so it will eliminate repeating permutations. I did a permutation that should of yielded 420 rearrangements, I ended up with 11 of the same for each original ( close to 5000 permutations ). I await the expertise of the all knowing one!
        That depends on what program you're using.

        If you're using the snippet from the perlfaq, you'll have to modify it somewhat. The easiest solution would be to return the values instead of printing it. In that case, my solution works as stands.

        The FAQ answer is recursive, so modifying it to use a hash internally is non-trivial, and it would take more time to change and to debug. Hope this helps.

        A reply falls below the community's threshold of quality. You may see it by logging in.
        I ran your program as printed. the following : a a b b b c d should have yielded 420 permutations. i get 4976 the same number as how...permut n..". The text you wrote says "To use it, first sort.... I'm a perl newbie,please give me layman details to make this program work w/o duplicate permutations and I will be eternally grateful to you!!!! email address swanhits@hotmail.com should you choose to be more direct. thanks waiting on your reply!!!