Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by LanX (Chancellor)
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

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?
[erix]: ha, good stuff - loud and fast...
[stonecolddevin]: here's some Mastodon for when you have 13 minutes to have your mind blown: https://www. youtube.com/watch? v=4pvfQtUhtNE
[karlgoethebier]: goes to Brownsville one more time..
erix squirrels away the linkies

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-06-22 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (531 votes). Check out past polls.