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

Hello Everyone,

Not sure how to name the title so sorry if it didn't make sense.

So I am still practicing/studying by making fun pointless codes so I can get use to Perl *first language* and I got curious on how to replace the long conditional statement. Of course I had some help, now I want to add a conditional statement to that shortcut but I don't know how...

Here is the snippet

sub menu { foreach my $keys (keys(%hash)) { my $num = 1; foreach my $race (@{$hash{$keys}}) { print "$num . $race\n"; $num++; } print "\nGive me your choice..\n"; $choice = ucfirst(<STDIN>); chomp $choice; $choice--; my $sub = "$hash{$keys}[$choice]" . "choice"; print "You have chose " . $hash{$keys}[$choice] . "!\ +n"; &{$sub}(); } }

I want to make something to where the numbers AND the STDIN work, only the number works. Say if tomato was the first key, I put 1 and it would work but I also want to make it where if they put in tomato it will also work. Instead if I put in any random answer, it always goes to the last key.

I was curious and I tried putting something like

 if ($choice eq $hash{keys}) {}

But of course it didn't work.. I want to do this because then I can put

 else { print "Please put in a valid answer\n"; &menu;}

And also accept the full "tomato" input than just the key "1"

I'm still in the beginning stage of learning and I got curious.. Thank you in advance! And I apologize if I didn't explain it well...

ALSO, the &{$sub}(); doesn't work with use strict;, is there a way to do this with strict?

.

Replies are listed 'Best First'.
Re: how to add conditionals to hash and stdin.
by choroba (Cardinal) on Mar 13, 2014 at 15:42 UTC
    You are after a dispatch table.
    #!/usr/bin/perl use warnings; use strict; my @actions = qw( add subtract quit ); my %dispatch = ( add => sub { my ($x, $y) = map int rand 10, 1, 2; print "$x + $y = ", $x + $y, "\n"; }, subtract => sub { my ($x, $y) = map int rand 10, 1, 2; print "$x - $y = ", $x - $y, "\n"; }, quit => sub { exit }, ); while (1) { for my $idx (0 .. $#actions) { print "$idx) $actions[$idx]\n"; } chomp(my $choice = <>); $choice = $actions[$choice] if $choice =~ /^[0-9]$/; if (exists $dispatch{$choice}) { $dispatch{$choice}->(); next } print "Invalid option.\n"; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to add conditionals to hash and stdin.
by NetWallah (Canon) on Mar 13, 2014 at 16:01 UTC
    Some of your code can be re-written for improved readability, and more perl-like:
    foreach my $keys (keys(%hash)) { my $races = $hash{$keys}; # For easy access to @$races + ... my $choice = "INVALID"; while (not ( $choice=~/^\d+$/ and defined $races->[$c +hoice -1])){ print ($_ +1) . " . " . $races->[$_] . "\n" for 0.. +$#$races; print "\nGive me your choice..\n"; chomp ( $choice = <STDIN>); } ....
    **UNTESTED **

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992