Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 chanting in the Monastery: (5)
As of 2015-07-02 23:58 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 (47 votes), past polls