in reply to permutations problem
If you like simple, try this:
#! perl -slw use strict; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CO +DE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } my @list1 = ( 1, 2 ); my @list2 = qw[ a b ]; my @list3 = qw[ # * & ]; ## Your code goes in the block below ## The list is the number of elements in each array ## Each time the block is called ## $_[0] is the index to use in the first list ## $_[1] " ## $_[2] 8 ## etc. nFor { print join '', $list1[ $_[0] ], $list2[ $_[1] ], $list3[ $_[2] ]; } 2,2,3; __END__ C:\test>795953.pl Possible attempt to put comments in qw() list at C:\test\795953.pl lin +e 23. 1a# 1a* 1a& 1b# 1b* 1b& 2a# 2a* 2a& 2b# 2b* 2b&
If your lists are arranged as a list of lists, things get a little tidier.
Update:To clarify, if your lists are arranged as an AoAs, then the problem generalises to:
my @lists = ( [ 1, 2 ], [ qw[ a b ] ], [ qw[ # * & ] ] ); nFor { print join '', map $lists[ $_ ][ $_[ $_ ] ], 0 .. $#_; } map scalar @$_, @lists;
Which handles any number of lists or items transparently.
|
|---|