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...
this below code does not work - while i can execute my program - nothing is getting returnedsub 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; } }
thanks.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; } }
Back to
Seekers of Perl Wisdom