Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am stuck on this code. This is the best way to describe what I am trying to do.
I have a form with 3 options.
First option will select just random 2 elements of array @acc.
Second option I could attach a random 2 values either from array @sec or @third or from both and print the result.
I am hoping to get something like this:
e.g. AX1 - AX3 ( random 2 elements from array @acc.)
or AX1PB7 AX2PB8 ( 2 random values of the array @acc with 2 random values from array @sec, and so on if I add the option to add 2 more random element from @third.
I hope it explains, here if a test code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Any Choice or combination coming from a form: my $test1 = "1"; my $test2 = ""; my $test3 = ""; my @acc = ('AX1', 'AX2', 'AX3',); my @sec = ('PB6', 'PB7', 'PB8',); my @third = ('XC2', 'XC8', 'XC1',); my $result; for (my $l = 0; $l < @acc; ++$l) { $result = $acc[rand @acc]; if($test2){ for (my $k = 0; $k < @sec; ++$k) { my$c2 = $acc[$l]; my$week2 = $sec[$k]; $result = $acc[$l].$sec[$k]; } } =code if($test3){ for (my $y = 0; $y < @third; ++$y) { my $days = $acc[$l]; my $week = $sec[$k]; my $holy = $third[$y]; $result = $acc[$l].$sec[$k].$third[$y]; } } =cut } print " $result\n";
Thanks for looking!

Replies are listed 'Best First'.
Re: Random number of elements from arrays
by choroba (Cardinal) on Sep 20, 2016 at 15:27 UTC
    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,
      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
Re: Random number of elements from arrays
by AnomalousMonk (Archbishop) on Sep 20, 2016 at 19:25 UTC

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

Re: Random number of elements from arrays
by kcott (Archbishop) on Sep 21, 2016 at 22:34 UTC

    Your response (in "Re^2: Random number of elements from arrays") has covered most ambiguities; however, it's still unclear whether you want random elements (as described in text) or unique random elements (as shown in all examples). For instance, you've shown "AX1 AX3" in two examples as "2 random elements from @acc"; but you haven't excluded "AX1 AX1" which would also be perfectly valid as "2 random elements from @acc". I'm assuming you want unique random elements.

    In the code below, I've used the fact that hash keys are unsorted (i.e. random) and the keys() function only returns each key once (i.e. unique). That's an oversimplification regarding the randomness of hash keys: see the documentation for more complete details. Also note that the code is only intended to demonstrate a technique: you'll need validation, error checking and so on to make it production-ready.

    #!/usr/bin/env perl -l use strict; use warnings; my @acc = qw{AX1 AX2 AX3}; my @sec = qw{PB6 PB7 PB8}; my @third = qw{XC2 XC8 XC1}; my %output_for_option = ( 1 => sub { print "@{get_rand_acc()}" }, 2 => sub { my ($acc, $sec) = (get_rand_acc(), get_rand_sec()); print "@{[ map { $acc->[$_] . $sec->[$_] } 0, 1 ]}"; }, 3 => sub { my ($acc, $third) = (get_rand_acc(), get_rand_third()); print "@{[ map { $acc->[$_] . $third->[$_] } 0, 1 ]}"; }, 4 => sub { my ($acc, $sec, $third) = (get_rand_acc(), get_rand_sec(), get_rand_third()); print "@{[ map { $acc->[$_] . $sec->[$_] . $third->[$_] } 0, 1 + ]}"; }, ); while (<>) { chomp; last unless exists $output_for_option{$_}; $output_for_option{$_}->(); } sub get_rand_acc { get_rand_from_array(\@acc) } sub get_rand_sec { get_rand_from_array(\@sec) } sub get_rand_third { get_rand_from_array(\@third) } sub get_rand_from_array { my %h = map { $_ => undef } @{+shift}; return [ (keys %h)[0, 1] ]; }

    Here's a couple of sample runs:

    $ pm_1172223_rand_array_concat.pl 1 AX3 AX1 2 AX2PB7 AX3PB6 3 AX3XC2 AX1XC8 4 AX1PB8XC2 AX3PB6XC8 5 $
    $ pm_1172223_rand_array_concat.pl 1 AX1 AX3 2 AX1PB7 AX2PB8 3 AX1XC1 AX3XC8 4 AX3PB6XC1 AX2PB8XC2 5 $

    — Ken