in reply to Random Values from Hash with UNLESS statement

How about this (taking Rolf's advice into account):

if( defined $self->{exits} ) { my @rooms = grep { $_ ne 'exit' } values $self->{exits}; return $rooms[ rand @rooms ]; }

Replies are listed 'Best First'.
Re^2: Random Values from Hash with UNLESS statement
by cmikel (Novice) on Apr 23, 2013 at 13:44 UTC
    thank you for the replies. i think i should have explained my setup a bit better. in my main.pl i have a hash of rooms which contains room object and direction object.
    my %rooms = ( frontyard => Rooms->new( 1, 'Front Yard', 'You\'re standing in the front yard looking at the rundown hou +se.', Directions->new( 'hallway', 'exit' ) ), hallway => Rooms->new( 2, 'Hallway', 'Long and narrow with flickering lights.', Directions->new( 'backyard', 'frontyard', 'bedroom', 'kitchen' + ) ), . . . .etc
    the directions object is a hash with the keys north, south, east, west respectively. the only 'room' that has an exit from the game is the front yard...(in the case the exit is south when standing in frontyard). so i do not want to generate 'exit' as the random room as it is not a room.

    i was thinking of using hash keys because if it was defined then it would generate a random number which then would corresponded to a random key/value pair....now i do see why this might not be the best approach...