Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Multiple Permutation handling

by ggoebel (Sexton)
on Mar 01, 2013 at 22:38 UTC ( #1021354=note: print w/ replies, xml ) Need Help??


in reply to Multiple Permutation handling

"Takes a list or array of array references and returns a function that will return successive permutations of the referenced arrays. Avoids recursion so will work on abitrarily huge sets of data. Runtime scales linearly with the number of sets. Minimal memory usage." -- shotgunefx w/ ariels

use strict; use warnings; use integer; my @data = ( ['SKU'], [1..5], [qw(T S L H)], [qw(S M L XL 2X)], [qw(BLU GRN WHT BLK)] ); my $iter = make_permutator(@data); my ($idx, @idx_link); while (my @els = $iter->() ){ print @els, "\n"; } sub make_permutator { @idx_link = (0, @_); return sub { $idx = $idx_link[0]++; my @ret; for my $i (1..$#idx_link) { push @ret, $idx_link[$i][$idx % @{$idx_link[$i]}]; $idx /= @{$idx_link[$i]}; } return $idx ? () : @ret; } } 1; __END__


Comment on Re: Multiple Permutation handling
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2014-12-26 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls