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:

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:  <%-{-{-{-<


In reply to Re: Random number of elements from arrays by AnomalousMonk
in thread Random number of elements from arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.