in reply to Random number of elements from arrays

Unfortunately, it's not clear what you want. Can the values be repeated? Also, you only describe what options 1 and 2 do, but not what option 3 does.

Here's a solution that doesn't repeat values from the arrays:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ shuffle }; my @acc = qw( AX1 AX2 AX3 ); my @sec = qw( PB6 PB7 PB8 ); my @third = qw( XC2 XC8 XC1 ); my @options = @ARGV; # i.e. run as "script.pl 1 0 1" to set options 1 + and 3 say join ' ', $options[0] ? (shuffle(@acc) )[ 0, 1 ] : (), # Ou +tput 2 random values from @acc. $options[1] ? (shuffle(@sec, @third))[ 0, 1 ] : (), # Ou +tput 2 random values from @sec and @third. $options[2] ? ('?') : (); # Replace ? by the expected ou +tput for option 3.

If you want to use one array in more than one option, you have to change the algorithm (you can splice a temporary copy of a shuffled array, or store the selected elements in a hash, or whatever).

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Random number of elements from arrays
by Anonymous Monk on Sep 20, 2016 at 15:47 UTC
    Its confused to explaing, but, this is what I amtrying to get.

    Result:
    option 1: AX1 AX3 - has 2 random elements from @acc
    option 2: AX1PB7 AX3PB6 - has 2 random from @acc and 2 random from @sec
    option 3: AX1XC2 AX2CX8 - has 2 random from @acc and 2 random from @third
    option 4: AX3PB6XC8 AX1PB6CX2 - has 2 random from @acc, 2 random elements from @sec and 2 random from @third

      Often you can clear up confusion by being clearer about the context of what you are trying to do. Give us the bigger picture and we can more easily paint in the details.

      Premature optimization is the root of all job security