2013-02-01 114 views
1

我正在寫一個簡單的程序,用於從文件讀取莫爾斯碼並將其轉換爲純文本。雖然我收到了一些瘋狂的錯誤。我對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}; 
} 

回答

1

記住,總是use strict;。在文件中行之有效多個... --- ...(所有的摩爾斯電碼,我知道):

#!/usr/bin/perl 

use 5.010; 
use strict; 
use warnings; 

my %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", ".-.-.-" => ".", "--..--" => ",", "..--.." => "?", ".----." => "'", 
"-....-" => "-", ".-..-." => '"', ".--.-." => "@", "-...-" => "=", "!" => " " 
); 

print "The message is \n"; 

while (<>) { 
    chomp; 
    foreach my $char (split ' ') { 
     print $morse_to_plain{$char}; 
    } 
    print "\n"; 
} 
+0

我沒有得到了錯誤,但它僅讀取文件的第一行。它需要讀取所有的人。 – JLott

+0

@JLott在標量上下文中使用'<>'只讀取一行。如果我是你,我會使用'while(<>)'循環並逐一讀取這些行。 – TLP

+0

@JLott - 我的歉意,因爲我沒有意識到你正試圖從文件中讀取。爲此調整了腳本。請考慮合併[TLP](http://stackoverflow.com/users/725418/tlp)的建議。 – Kenosis

7

您正在閱讀的是沒有在您的哈希匹配的鑰匙串,因此哈希值是不確定的(未初始化) 。這可能是一個輸入問題。試試這個調試目的:

print $morse_to_plain{$char} // "Key does not exist: '$char'\n"; 

對於更長的字符串,你可能會考慮這一點:

$string =~ s{([-.]+)}{ $morse_to_plain{$1} // $1 }ge; 

這將搜索點和線的組合,並把它們翻譯成自己的文本等價的,還是自己,如果沒有翻譯被發現。

你也應該考慮讓你的哈希分配多一點可讀性:

my %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", ".-.-.-" => ".", "--..--" => ",", "..--.." => "?", ".----." => "'", 
    "-....-" => "-", ".-..-." => '"', ".--.-." => "@", "-...-" => "=", "!"  => " " 
); 

它將使錯別字很多比較容易辨認。此外,您還可以使反向查找表相當容易:

my %plain_to_morse = reverse %morse_to_plain; 
+0

非常棒!或者將一個字符(或代碼)替換爲「未知代碼」的佔位符。 – Kenosis

+0

偉大的建議!我也會用它來做很多其他的事情。 – JLott

相關問題