Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Random Values from Hash with UNLESS statement

by cmikel (Novice)
on Apr 23, 2013 at 00:58 UTC ( [id://1030011]=perlquestion: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re: Random Values from Hash with UNLESS statement
by LanX (Saint) on Apr 23, 2013 at 01:32 UTC
    Why do you choose a random key to find a random value? =)

    pick directly a random value:

    DB<127> $self->{exits}= { kitchen => 42, living => 666 , toilet => +"00" } => { kitchen => 42, living => 666, toilet => "00" } DB<128> @values= values %{$self->{exits}} => (666, "00", 42) DB<129> $rand_value = @values[rand @values] => "00" DB<130> $rand_value = @values[rand @values] => 42 DB<131> $rand_value = @values[rand @values] => 42 DB<132> $rand_value = @values[rand @values] => 666

    If you still wanna go this way, please DRY.

    Replace my $h_ref = $self->{exits} and you can avoid many repetitions of $self->{exits}

    DB<134> $h_exits = $self->{exits} => { kitchen => 42, living => 666, toilet => "00" } DB<135> $random_room = $h_exits->{ ( keys %$h_exits )[ rand keys %$h +_exits ] } => "00"

    Better readable now! Don't you agree? =)

    > this below code does not work

    sure unless('exit') is always false.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Random Values from Hash with UNLESS statement
by hdb (Monsignor) on Apr 23, 2013 at 06:13 UTC

    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 ]; }
      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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1030011]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found