http://www.perlmonks.org?node_id=1030011

cmikel has asked for the wisdom of the Perl Monks concerning the following question:

hello. rather new to perl and i am back for more wisdom. i would like to return a random value from my hash UNLESS that random value eq 'exit' this below code does the trick - however i think it could be done in a more simplistic fashion and be more dynamic...
sub get_random_room { my $self = shift; my $random_room = ''; if ( defined $self->{exits} ) { $random_room = $self->{exits}{ ( keys %{$self->{exits}} ) +[ rand keys %{$self->{exits}} ] }; #no random exiting if ($random_room eq 'exit') { $random_room = 'hallway'; } return $random_room; } }
this below code does not work - while i can execute my program - nothing is getting returned
sub get_random_room { my $self = shift; my $random_room = ''; if ( defined $self->{exits} ) { $random_room = $self->{exits}{ ( keys %{$self->{exits}} ) +[ rand keys %{$self->{exits}} ] } unless ('exit'); return $random_room; } }
thanks.