Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Pick random item from list of unknown length?

by Cody Fendant (Pilgrim)
on Mar 01, 2010 at 06:03 UTC ( #825834=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Pick random item from list of unknown length?
in thread Pick random item from list of unknown length?

That works with the implicit regex on $_

$_ = 'apple,banana,cherry'; print "Say ", (m!([^,]+)!g)[ scalar rand 1 + (tr!,!!) ], "\n";
but for some reason, when I do this:
$foo = 'apple,banana,cherry'; print "Say ", ($foo =~ m!([^,]+)!g)[ scalar rand 1 + (tr!,!!) ], "\n";

I only ever get 'apple'.


Comment on Re^3: Pick random item from list of unknown length?
Select or Download Code
Re^4: Pick random item from list of unknown length?
by Anonymous Monk on Mar 01, 2010 at 07:17 UTC
    That works with the implicit regex on $_

    Nope, its implicit m//atch on $_, and you're forgetting about the implicit tr///ansliteration on $_

      Oh right. Slap-forehead and all that.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-08-23 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls