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 | |
|
Re: how to add conditionals to hash and stdin.
by NetWallah (Canon) on Mar 13, 2014 at 16:01 UTC |