Ask User for Input and See if The String Matches Strings in Hash
Hi,
I need help with Hash&Else if in Perl
In a hash, a=>1, b=>2, c=>3.
I need to write a script that will ask user for input. if the user typed in a or b or c, the output will display 1 or 2 or 3 respectively. Else, print "not valid".
Below is my code but I got errors.
#!/usr/bin/perl
use warnings;
my %data =(
'a' => 1,
'b' => 2,
'c' => 3
);
print "data: ";
chomp ($word = <STDIN>);
if ($word eq $data){
print ("$data{'$word'}\n");
} else {
print "not valid\n";
}
Thanks!
|