Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I permute N elements of a list?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #618=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

Here's a little program that generates all permutations of all the words on each line of input. The algorithm embodied in the permute() function should work on any list:

    #!/usr/bin/perl -n
    # tsc-permute: permute each word of input
    permute([split], []);
    sub permute {
        my @items = @{ $_[0] };
        my @perms = @{ $_[1] };
        unless (@items) {
            print "@perms\n";
        } else {
            my(@newitems,@newperms,$i);
            foreach $i (0 .. $#items) {
                @newitems = @items;
                @newperms = @perms;
                unshift(@newperms, splice(@newitems, $i, 1));
                permute([@newitems], [@newperms]);
            }
        }
    }

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Corion]: Hmm - is anybody aware of a "protocol-only" HTTP module? I'd like to teach Prima non-blocking HTTP ideally without using threads, but that means reimplementing/ rewriting something like HTTP::Tiny (except asynchronous) or AnyEvent::HTTP
[Corion]: Ideally, the HTTP state machine but with a way to feed it HTTP data whenever I get it instead of it actively doing stuff.
[Corion]: Now that I talk about this, maybe Net::Inspect::L7:: HTTP would be the right kind of thing for the responses at least.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2016-12-07 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (128 votes). Check out past polls.