[问题] 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