我正在寫一個簡單的程序,用於從文件讀取莫爾斯碼並將其轉換爲純文本。雖然我收到了一些瘋狂的錯誤。我對perl不是很熟悉,我不得不從命令行運行它。以下是我收到的錯誤和代碼。我可能錯了。我在命令行輸入:「perl -w Lott_Morse.pl morse.txt」。任何幫助,將不勝感激。Perl - 打印中未初始化的值導致的錯誤
錯誤:
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
Use of uninitialized value in print at Lott_CSC360_Morse2.pl line 31, <> line 7.
The message is 0Jessicas-MacBook-Pro:Documents
代碼:
#!/usr/bin/perl
use 5.010;
use warnings;
%morse_to_plain=(
".-" =>"A", "-..." => "B", "-.-." => "C", "-.." => "D", "." => "E",
"..-." => "F", "--." => "G", "...." => "H", ".." => "I", ".---" => "J",
"-.-" => "K", ".-.." => "L", "--" => "M", "-." => "N", "---" => "O",
".--." => "P", "--.-" => "Q", ".-." => "R", "..." => "S", "-" => "T",
"..-" => "U", "...-" => "V", ".--" => "W", "-..-" => "X", "-.--" => "Y",
"--.." => "Z", "-----" => "0", ".----" => "1", "..---" => "2", "...--" => "3",
"....-" => "4", "....." => "5", "-...." => "6", "--..." => "7", "---.." => "8",
"----." => "9", ".-.-.-" => ".", "--..--" => ",", "..--.." => "?", ".----." => "'",
"-....-" => "-", ".-..-." => '"', ".--.-." => "@", "-...-" => "=", "!" => " "
);
chomp(@message = <>);
print "The message is ";
foreach $char (@message)
{
print $morse_to_plain{$char};
}
我沒有得到了錯誤,但它僅讀取文件的第一行。它需要讀取所有的人。 – JLott
@JLott在標量上下文中使用'<>'只讀取一行。如果我是你,我會使用'while(<>)'循環並逐一讀取這些行。 – TLP
@JLott - 我的歉意,因爲我沒有意識到你正試圖從文件中讀取。爲此調整了腳本。請考慮合併[TLP](http://stackoverflow.com/users/725418/tlp)的建議。 – Kenosis