Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Random Values from Hash with UNLESS statement

by cmikel (Novice)
on Apr 23, 2013 at 00:58 UTC ( #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; } }

Replies are listed 'Best First'.
Re: Random Values from Hash with UNLESS statement
by LanX (Bishop) 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 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 i do see why this might not be the best approach...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030011]
Approved by davido
[Discipulus]: I havery limited needs in such field. Sometimes (for children party invitations) i draw something by hand and I scan it to include into an ms doc.
Discipulus havery can be a good neologism for: have very..;=)
[ambrus]: Discipulus: sure, it's just that, look, some people (including me) have such unreadable and slow handwriting that writing an article or slides by hand would be a very bad idea.
[choroba]: it might bery useful for other verbs, too
[ambrus]: (and I have a need for at least decent formatting too)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-09-26 11:30 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.