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:
So if you can figure out which "option" in the '1' to '4' range you want, you're all set.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
Note that:
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):
Just another way...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; }
Give a man a fish: <%-{-{-{-<
In reply to Re: Random number of elements from arrays
by AnomalousMonk
in thread Random number of elements from arrays
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |