perl - Perl-由打印中未初始化的值引起的错误

标签 perl file-io compiler-errors terminal padre

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

最佳答案

请记住始终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";
}

关于perl - Perl-由打印中未初始化的值引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14638106/

相关文章:

perl - 在 Perl 中获取十六进制字符串的 SHA1 HMAC

Perl:什么时候释放不需要的标量内存而不超出范围?

java - BufferedReader 如何跟踪已读取的行?

c - 如何从文本文件中读取多个数字

c++ - 在 C++ 代码部分,可能会写入两个字节

当 "src"值不同时,捕获 <img> 标签的正则表达式失败

python pdb 相当于可调用的 perldb 方法?

C++ 在 Unicode 而不是 Ansi 中创建文件

compiler-errors - 程序不包含适用于入口点的静态 'Main'方法///

ios - ld : truncated fat file