我正在编写一个简单的程序,从文件中读取莫尔斯电码并将其转换为纯文本。不过,我遇到了一些疯狂的错误。我对perl不是很熟悉,我必须从命令行运行它。以下是我收到的错误和代码。我可能只是跑错了。我正在输入:"perl -w Lott_Morse.pl morse.txt"到命令行中。任何帮助将不胜感激。
错误:
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};
}
您正在读取的字符串中没有哈希中的匹配键,因此哈希值未定义(未初始化)。这可能是输入问题。出于调试目的,请尝试此操作:
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;
记得总是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";
}