Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Idiom to return 0 or random number of array elements

by LanX (Canon)
on Dec 07, 2012 at 05:48 UTC ( #1007690=note: print w/ replies, xml ) Need Help??


in reply to Idiom to return 0 or random number of array elements

just include the possibility of @array[0..-1] which returns an empty list.

try something like: @array[0 .. (int rand (@array+1)) -1]

DB<130> @array=a..c => ("a", "b", "c") DB<131> @array[0 .. -1 + int rand 1+ @array] => () DB<132> @array[0 .. -1 + int rand 1+ @array] => ("a", "b") DB<133> @array[0 .. -1 + int rand 1+ @array] => ("a") DB<134> @array[0 .. -1 + int rand 1+ @array] => ("a", "b", "c") DB<135> @array[0 .. -1 + int rand 1+ @array] => ("a", "b")

Cheers Rolf


Comment on Re: Idiom to return 0 or random number of array elements
Select or Download Code
Replies are listed 'Best First'.
Re^2: Idiom to return 0 or random number of array elements
by Anonymous Monk on Dec 07, 2012 at 06:04 UTC
    Perfect, that's what I was looking for. Guess I was close, though. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007690]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2015-07-29 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls