perlfile-iocompiler-errorsterminalpadre

Perl - Error caused by uninitialized value in print


I am writing a simple program that reads morse code from a file and converts it to plain text. I am getting some crazy errors though. I am not very familiar with perl and I am having to run it from command line. Below is the error that I am receiving and the code. It is possible that I am just running it wrong. I am typing: "perl -w Lott_Morse.pl morse.txt" into the command line. Any help would be appreciated.

Error:

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

Code:

#!/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};
}

Solution

  • Remember to always use strict;. Worked well on multiple ... --- ... in a file (all the Morse code I know):

    #!/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";
    }