Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by LanX (Saint)
on Dec 07, 2012 at 05:48 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1007690]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found