1FastPermute(3) User Contributed Perl Documentation FastPermute(3)
2
3
4
6 Algorithm::FastPermute - Rapid generation of permutations
7
9 use Algorithm::FastPermute ('permute');
10 my @array = (1..shift());
11 permute {
12 print "@array\n"; # Print all the permutations
13 } @array;
14
16 Algorithm::FastPermute generates all the permutations of an array. You
17 pass a block of code, which will be executed for each permutation. The
18 array will be changed in place, and then changed back again before
19 "permute" returns. During the execution of the callback, the array is
20 read-only and you'll get an error if you try to change its length. (You
21 can change its elements, but the consequences are liable to confuse you
22 and may change in future versions.)
23
24 You have to pass an array, it can't just be a list. It does work with
25 special arrays and tied arrays, though unless you're doing something
26 particularly abstruse you'd be better off copying the elements into a
27 normal array first.
28
29 It's very fast. My tests suggest it's four or five times as fast as
30 Algorithm::Permute's traditional interface. If you're permuting a
31 large list (nine or more elements, say) then you'll appreciate this
32 enormously. If your lists are short then Algorithm::Permute will still
33 finish faster than you can blink, and you may find its interface more
34 convenient.
35
36 In fact, the FastPermute interface (and code) is now also included in
37 Algorithm::Permute, so you may not need both. Enhancements and bug
38 fixes will appear here first, from where (at Edwin Pratomo's
39 discretion) they'll probably make their way into Algorithm::Permute.
40
41 The code is run inside a pseudo block, rather than as a normal
42 subroutine. That means you can't use "return", and you can't jump out
43 of it using "goto" and so on. Also, "caller" won't tell you anything
44 helpful from inside the callback. Such is the price of speed.
45
46 The order in which the permutations are generated is not guaranteed, so
47 don't rely on it.
48
50 The "permute" function is exported by default.
51
53 Robin Houston, <robin@kitsite.com>
54
55 Based on a C program by Matt Day.
56
58 Algorithm::Permute
59
61 Copyright (c) 2001-2008, Robin Houston. All Rights Reserved. This
62 module is free software. It may be used, redistributed and/or modified
63 under the same terms as Perl itself.
64
65
66
67perl v5.36.0 2023-01-19 FastPermute(3)