Perl - Error caused by uninitialized value in print

913 views Asked by At

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};
}
2

There are 2 answers

4
Kenosis On BEST ANSWER

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";
}
2
TLP On

You are reading in a string that does not have a matching key in your hash, so the hash value is undefined (uninitialized). It is likely an input problem. Try this for debugging purposes:

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

For longer strings, you might consider this:

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

Which will search for combinations of dots and dashes and translate them to their text equivalent, or themselves if no translation is found.

You should also consider making your hash assignment a bit more readable:

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

It will make typos a lot easier to spot. Also, you can make the inverted lookup table rather easily:

my %plain_to_morse = reverse %morse_to_plain;