SHKVal has asked for the wisdom of the Perl Monks concerning the following question:
where @test is a 2-dimentional array.sub permutations { my @array = @_; #my @tmp = @_; #if ($tmp[0][1] != 0) { for (0..$#{$tmp[0]}) { push (@array, $tmp[0] +[$_]); } } #else { @array = @_; } if ($#array == 0) {return [ $array[0] ]; } my @results; my $element; foreach $element (0..$#array) { my @leftovers = @array; my $chosen_one = splice(@leftovers, $element, 1); foreach (&permutations(@leftovers)) { push(@results, [ $chosen_one, @{$_} ]); } } return @results; } @a = permutations($test[2]);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with passing array in a sub
by choroba (Cardinal) on May 30, 2014 at 08:15 UTC | |
|
Re: Problem with passing array in a sub
by perlfan (Parson) on May 30, 2014 at 12:16 UTC | |
|
Re: Problem with passing array in a sub
by RonW (Parson) on May 30, 2014 at 16:47 UTC | |
|
Re: Problem with passing array in a sub
by SHKVal (Initiate) on May 30, 2014 at 14:32 UTC |