in reply to Random Values from Hash with UNLESS statement
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)
|
|---|