On Thu, 29 Jul 2004, Paul Lalli wrote:
> > ok...sorry:
> >
> > i have got this:
> >
> > my @test = (a,b,c);
> > my $length = 4;
> >
> > foreach $letter(@test){
> > for (my $i =0; $i<=3; $i++){
> > $out = $letter.$test[$i];
> > push (my @array, $out);
> > }
> > }
> > return @array;
> >
> > I hope it's more clear now.
>
> Mildly. Did you look at the "permute" documentation I posted earlier? It
> should be possible to do this fairly easily using the List:
ermutor
> module it refers to...
I got bored while waiting for some clients at work... This is my first
attempt. It's not at all efficient, and I'm sure others here would be
able to make it much cleaner. But it does produce (what I believe to be)
your desired output:
#!/usr/bin/env perl
use strict;
use warnings;
use List:

ermutor;
my @data = qw/a b c d/;
my %results; #where we'll store each string we find
sub perm {
return unless @_; #base case for recursion is just to exit
my $perm = new List:

ermutor @_;
while (my @tempresults = $perm->next){
#join each subset into a string, and record it
$results{join '', @tempresults}++;
}
#remove one element at a time from data, and permute the result
for my $i (0 .. $#_){
my @temp = @_;
splice @temp, $i, 1;
perm(@temp);
}
}
#start everything off
perm (@data);
#Get one copy each of each string found.
my @results = sort keys %results;
print "@results\n";
__END__
a ab abc abcd abd abdc ac acb acbd acd acdb ad adb adbc adc adcb b ba bac
bacd bad badc bc bca bcad bcd bcda bd bda bdac bdc bdca c ca cab cabd cad
cadb cb cba cbad cbd cbda cd cda cdab cdb cdba d da dab dabc dac dacb db
dba dbac dbc dbca dc dca dcab dcb dcba