in reply to oneliner for selecting multiple values from array

my @arr = ('A'..'Z'); my $combo = join '', @arr[rand @arr, rand @arr]; print $combo . "\n";

see Slices

Replies are listed 'Best First'.
Re^2: oneliner for selecting multiple values from array (shuffle)
by toolic (Bishop) on Nov 18, 2008 at 15:06 UTC
    Another way using the core module List::Util
    use List::Util qw(shuffle); my @arr = ('A'..'Z'); my $combo = join '', (shuffle(@arr))[0..1]; print "$combo\n";
      Your solution, toolic, is nice but not equivalent to the original script; the original could choose two elements that are equal; yours can't... ;-)
      []s, HTH, Massa (κς,πμ,πλ)
        Fair enough -- I guess I focussed more on the description rather than the code of the OP:
        Is there a way to select multiple values from the array without repetition?
        I interpreted the description as meaning "unique" elements of the array, which my solution will provide. So, there seems to be a discrepancy between the OP description and code.
Re^2: oneliner for selecting multiple values from array
by gone2015 (Deacon) on Nov 18, 2008 at 19:22 UTC

    A little more generally you could:

    my @arr = ('A'..'Z'); my $n = 2 ; my $combo = join '', map { $arr[rand @arr] } (1..$n) ; print $combo . "\n";
    which will give you any length string you like.

    Whatever you do, don't do:

    my @arr = ('A'..'Z'); my $n = 2 ; my $combo = $arr[rand @arr] x $n ; print $combo . "\n";
    however plausible it might appear :-(

    Of course it doesn't work, you say, it's obvious... However, I have, ahem, a friend who once (once was enough):

    my @arr = ([]) x $count ;
    and had a lot of fun (tm) working out why their program behaved as oddly as it did !

Re^2: oneliner for selecting multiple values from array
by ev00rg (Initiate) on Nov 18, 2008 at 14:14 UTC
    many thanks!