Shuffle the elements of an array in random order.
Arrays in Perl 6 have the
pick method, which does the work.
my @a = 1..20;
A possible output of the program looks like this:
(4 18 10 15 14 8 2 11 3 12 1 6 9 19 13 7 16 17 20 5)
pick method expects an integer argument that defines the number of picked elements. In the example above, passing the array as an argument causes Perl to coerce it into an integer by calling the
@a.Int method, which returns the length of the array.
After the operation, the original data remains unchanged. If you need to update the
@a variable, use the
.= operator to call a method and assign its result to the invocant:
Elements of the array are not repeated in the output.
In Perl 6, arrays also have the
roll method, which works similar but does not guarantee that the elements are not repeated.
Calling either pickor rollwith no argument returns a single element from an array. If the value of the argument is bigger than the length of the array, the
pick method returns the list of the same size as the original one, while
roll happily generates more repeated items.