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

看板Perl作者 (萬里無雲萬里天)時間11年前 (2013/07/04 08:06), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
想寫個程式可以印出一個月的月曆 像這樣 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; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 67.169.183.176 ※ 編輯: freemirage 來自: 67.169.183.176 (07/04 08:08)

07/04 09:36, , 1F
自己找到解法了 原來是要用chomp處理
07/04 09:36, 1F
文章代碼(AID): #1HrBnvXl (Perl)