I'm also a bit confused about what you want (e.g., I don't understand the relevance of the AX1 AX3 AX1PB7 AX3PB6 ... business), but here, based on choroba's approach, is a dispatch table approach:
c:\@Work\Perl\monks>perl -le
"use warnings;
use strict;
;;
use List::Util qw{ shuffle };
;;
sub shuf_shuf_2_from_each (\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@);
;;
my @acc = qw( AX1 AX2 AX3 );
my @sec = qw( PB6 PB7 PB8 );
my @third = qw( XC2 XC8 XC1 );
;;
my %option = (
1 => sub { return shuf_shuf_2_from_each @acc; },
2 => sub { return shuf_shuf_2_from_each @acc, @sec; },
3 => sub { return shuf_shuf_2_from_each @acc, @third; },
4 => sub { return shuf_shuf_2_from_each @acc, @sec, @third; },
);
;;
for my $opt (1 .. 4) {
my @selected = $option{$opt}->();
print qq{option $opt: @selected};
}
;;
;;
sub shuf_shuf_2_from_each (\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
return shuffle map { (shuffle @$_)[0,1] } @_;
}
"
option 1: AX1 AX2
option 2: PB8 AX3 PB7 AX1
option 3: XC8 AX3 AX1 XC2
option 4: XC2 PB8 AX2 PB7 XC8 AX3
So if you can figure out which "option" in the '1' to '4' range you want, you're all set.
Note that:
-
After randomly selecting a pair of elements from each of the given input arrays, shuf_shuf_2_from_each() shuffles the selected pairs again before returning the list. If you don't want this extra shuffle, it's easy enough to get rid of it.
-
The prototype (please see Prototypes in perlsub; see also Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen) "hack" I've used in shuf_shuf_2_from_each() is not scalable. If you have more input arrays to shuffle than the number of \@s I've put in the prototype, the code won't compile. Again, easy enough to fix, but a bit inelegant. (If you absolutely don't like prototypes, there's an absolutely scalable way to avoid them.)
-
Update: Because shuffle() and fixed indices are used to randomly select a pair of elements from an input array, the duplication of elements selected from the array, e.g., AX2 AX2 from @acc, is not possible; the selected elements are always unique.
Update: Changed the for my $opt (1 .. 4) { ... } output display loop in the example code to make the whole process slightly more clear. (Just couldn't leave it alone...)
Update 2: Another thought is that if the list of input arrays is really the only thing that varies from option to option, maybe this is all that should be in the dispatch table and dispatching subroutines is overkill (this also does away with prototypes):
my %option = (
1 => [ \ @acc ],
2 => [ \(@acc, @sec) ],
3 => [ \(@acc, @third) ],
4 => [ \(@acc, @sec, @third) ],
);
...
my $opt = whatever();
my @selected = shuf_shuf_2_from_each($option{$opt});
...
sub shuf_shuf_2_from_each {
my ($ar_input_arrays) = @_;
return shuffle map { (shuffle @$_)[0,1] } @$ar_input_arrays;
}
Just another way...
Give a man a fish: <%-{-{-{-<
|