Subject: | Proposed new function: lucky_dip (randomly selected item from list) |
Hi,
I've used shuffle in a number of places, and just now wanted to select one item at random from a list.
my @messages = (
"Well done!",
"Awesome job",
"Idiot!"
);
my $message = pick @messages;
you could add a parameter, so pick($n, @messages), which also addresses RT#105275
This is a trivial thing to do, but I was sure there would be a function for this in List::Util. Turns out there isn't.
Was trying to think of a short and obvious name, like "pick", or "lucky_dip", but haven't yet come up with one that feels dead right. "pick" seems to match the existing terse naming, but "lucky_dip" more obviously identifies the function.
Submitting as a separate ticket as the "lucky_dip" function would be best as:
my $message = lucky_dip @messages;
But pick would probably be best as:
my ($message) = pick 1, @messages;