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'.

Replies are listed 'Best First'.
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.