perl - Use of uninitialized value in print function -


i running following simple perl program.

use warnings; use strict;  %a = (b => "b", c => "c");  print "enter b or c: ";  $input = <stdin>;  print "the letter entered is: ", $input, "\n";  $d = $a{$input};  print ($d); 

when entered b got following output warning. line 47 last statement print ($d);

enter b or c: b letter entered is: b  use of uninitialized value $d in print @ c:/users/lzhang/workspace/perl5byexample/exer5_3.pl line 47, <stdin> line 1. 

why warning , how fix it?

your $input contains new line character besides b or c. modify trim charcter:

my $input = <stdin>;        # 1. $input "b\n" or "c\n"                              chomp $input;               # 2. rid of new line character                             #    $input "b" or "c"  print "the letter entered is: ", $input, "\n"; 

Comments