We don't bite newbies here... much PerlMonks

### Re: Non- recursive permutation of arrays.

by ariels (Curate)
 on Mar 25, 2002 at 08:08 UTC ( #154043=note: print w/replies, xml ) Need Help??

in reply to Non- recursive permutation of arrays.

Or, you could just count up to the product of the array sizes, and use the count to compute the offsets into the arrays (I think a note described this, but no code). If you have lists of lengths l1,...,lk, think of the numbers 0..(l1*...*lk-1). You want to convert them to a variable-base number system, where the first digit can be 0..l1-1, the second digit can be 0..l2-1,...

```#!/usr/local/bin/perl -w
use strict;

my @aoa = (
[('a'..'f')],
[('A'..'C')],
[(1..2)],
);

my \$iter = make_permutator(@aoa);

while (my @els = \$iter->() ){
print "@els\n";
}

# ariels' code from here
sub make_permutator {
use integer;
my @idx_link = (0, @_);
return sub {
my \$idx = \$idx_link[0]++;
my @ret;
for my \$i (1..\$#idx_link) {
}
return \$idx ? () : @ret;
}
}

Replies are listed 'Best First'.
Re: Re: Non- recursive permutation of arrays.
by shotgunefx (Parson) on Mar 25, 2002 at 10:28 UTC
Interesting take. That way solution wouldn't have occured to me. I do think it's a bit more obfu though. I had to stare at it for a minute (Maybe just me though!). The best thing (IMHO) about Perl is how many different ways you can accomplish a single goal.

-Lee

"To be civilized is to deny one's nature."

Create A New User
Node Status?
node history
Node Type: note [id://154043]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-10-30 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (278 votes). Check out past polls.

Notices?