1

我正在编写一个简单的程序,它从文件中读取莫尔斯电码并将其转换为纯文本。不过,我遇到了一些疯狂的错误。我对 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};
}
4

2 回答 2

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;
于 2013-02-01T01:28:13.890 回答
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";
}
于 2013-02-01T01:24:59.257 回答