in reply to Random number of elements from arrays
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 | |
by GrandFather (Saint) on Sep 20, 2016 at 21:17 UTC |