The reason that it is more efficient than the FAQ is that it spends as little energy copying data as possible.
#!/usr/bin/perl -n # tsc-permute: permute each word of input use subs qw( _permute ); permute(split); { my @perm; my @unperm; sub permute { @perm = (); @unperm = @_; _permute; } sub _permute { if (@unperm) { push @perm, pop @unperm; foreach (1..@perm) { push @perm, shift @perm; _permute; } push @unperm, pop @perm; } else { print "@perm\n"; } } }
In reply to Permuting n elements by tilly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |