-1

Coding

print "fruit list\n";
print "1.\tApple\n";
print "2.\tOrange\n";
print "3.\tPic\n";
print "3.\tBanana\n";
print "Based on fruit list above, please key in your favorite fruit name.\n";

%fruit_list = (
    1 => 'Apple',
    2 => 'Orange',
    3 => 'Pic',
    4 => 'Banana'
);

$fruit = $fruits[<STDIN>];

if ( $fruit == $fruit_list{'1'} ) {
    func1();
}
elsif ( $fruit == $fruit_list{'2'} ) {
    func2();
}

sub func1 {
    print "executing function 1\n";
}

sub func2 {
    print "executing function 2\n";
}

Output (inputting 1):

fruit list
1.      Apple
2.      Orange
3.      Pic
4.      Banana
Based on fruit list above, please key in your favorite fruit name.
1
executing function 1

Output (inputting 2):

fruit list
1.      Apple
2.      Orange
3.      Pic
4.      Banana
Based on fruit list above, please key in your favorite fruit name.
2
executing function 1
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 4
    Add `use strict; use warnings;` at the top, run it again, then fix all errors. – melpomene May 08 '18 at 08:36
  • For how to compare strings in perl, see https://stackoverflow.com/questions/1175390/how-do-i-compare-two-strings-in-perl – Doug May 08 '18 at 10:06

3 Answers3

5

There is no @fruits array, so $fruit = $fruits[<STDIN>] makes no sense. $fruit will be set to undef which evaluates as zero in a numeric context

Meanwhile, $fruit_list{'1'} is Apple, which also evaluates as zero in numeric context

The == operator compares numbers, so if ($fruit == $fruit_list{'1'}) will compare zero with zero and always find a match

You must have seen warning messages when you run your program. You must never ignore such messages as they are there to help you

You must add use strict and use warnings 'all' to the the top of every Perl program you write, and fix all resulting messages before asking others for help

You must also lay your code out properly, including indentation, so that other people (and yourself) can read it easily. I have edited the code in your question: please do it yourself in future, at least before asking other people for help with your code

Borodin
  • 126,100
  • 9
  • 70
  • 144
1

Seems like you are comparing strings using == operator here: if ($fruit == $fruit_list{'1'}){ and here: elsif ($fruit == $fruit_list{'2'}){.

In Perl, == is a numerical comparison operator, for comparing strings use eq operator (i.e., if ($fruit eq $fruit_list{'1'}){).

eyevan
  • 1,475
  • 8
  • 20
0

Thanks for the inputs of comparing strings and array declaration. I had taken the inputs to modify my code as follow and it's working as expected.

print "fruit list\n";
print "1.\tApple\n";
print "2.\tOrange\n";
print "3.\tPic\n";
print "3.\tBanana\n";
print "Based on fruit list above, please key in your favorite fruit name.\n";
my %fruit_list = (
    1 => 'Apple',
    2 => 'Orange',
    3 => 'Pic',
    4 => 'Banana'
    );
@values = values(%fruit_list);
my $fruit = $values [<STDIN>];
if ($fruit eq $values[1]){
func1();
}
elsif ($fruit eq $values[2]){
func2();
}

sub func1 {
print "executing function 1\n";
}

sub func2 {
print "executing function 2\n";
}

Output (inputting 2)

fruit list
1.      Apple
2.      Orange
3.      Pic
4.      Banana
Based on fruit list above, please key in your favorite fruit name.
2
executing function 2