[問題] STDIN的值對應不到hash的key

作者: freemirage (萬里無雲萬里天)   2013-07-04 08:06:13
想寫個程式可以印出一個月的月曆
像這樣
July
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
目前卡在無法讓使用者自己輸入月份
如果寫 $month = <STDIN>;
會跳出這樣的錯誤訊息
Use of uninitialized value within %days_in_month in foreach loop entry
at line 35, <STDIN> line 2.
小弟是新手 請多多指教
程式碼如下 錯誤訊息講的地方用黃色標起來了
#!/usr/bin/perl
use strict;
use warnings;
use feature ':5.10';
print "Please enter the month: ";
my $month = <STDIN>;
say "What's the first day of this month? ";
say "Sunday=0, Monday=1 ... Saturday=6";
my $firstday_in_month = <STDIN>;
my %weekday = (0 => "Sun", 1 => "Mon", 2=> "Tue", 3=>"Wed", 4 => "Thu", 5 =>
"Fri", 6 => "Sat");
my %days_in_month = ("January" => 31, "February" => 28, "March" => 31,
"April" => 30, "May" => 31, "June" => 30, "July" => 31, "August" => 31,
"Septembr" => 30, "October" => 31,
"November" => 30, "December" => 31);
my $j = $firstday_in_month; # This variable counts when to enter a new line.
# print the title
print $month, "\n";
for my $i (0 .. 6){
print $weekday{$i}, "\t", "\t";
}
print "\n";
#print each day of the month
if ($firstday_in_month != 0){
for (1 .. $firstday_in_month){
print "\t", "\t";
}
}
for my $i(1 .. $days_in_month{$month}) {
print $i, "\t", "\t";
if ($j%7 ==6){
print "\n";
}
$j +=1;
}
作者: freemirage (萬里無雲萬里天)   2013-07-04 09:36:00
自己找到解法了 原來是要用chomp處理

Links booklink

Contact Us: admin [ a t ] ucptt.com