Logo

Programming-Idioms

History of Idiom 11 > diff from v43 to v44

Edit summary for version 44 by Roboticus:
[Perl] Bugfix: moved quote, changed variable name to match problem description

Version 43

2016-04-20, 20:26:13

Version 44

2016-04-21, 10:20:55

Idiom #11 Pick a random element from a list

List x must be non-empty.

Idiom #11 Pick a random element from a list

List x must be non-empty.

Code
my @list = ('a', 'list', 'of', 'random', 'item's);

print $list[ rand(@list) ], "\n";
Code
my @x = ('a', 'list', 'of', 'random', 'items');

print $list[ rand(@x) ], "\n";
Comments bubble
In scalar context (such as the call to rand), an array evaluates to the length.
Comments bubble
In scalar context (such as the call to rand), an array evaluates to the length.