in reply to Get random element from HoA

If you wanted a random element from a random array (which I think is what you're aiming at):

return do { my $arry = $hoa{ (keys %hoa)[rand %hoa] }; $arr->[ rand @$ +arry ] };

Or possibly:

return $hoa{ my $key = (keys %hoa)[ rand %hoa ] }->[ rand @{$hoa{$key} +} ];

I can't think of a clean way to do it without some temporary or building a parallel data structure (containing either the indexes or a copy of all the elements).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Get random element from HoA
by Anonymous Monk on Dec 27, 2007 at 18:04 UTC
    Wow, is that a "my" declaration in the hash key? That's new to me, thanks :)