First Perl Program trouble
Can anybody tell me why this doesn't want to run:
#!/usr/bin/perl -w
# Build Morse Code Associtive Array
%MorseCode = ( "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" => "--..",
" " => "/");
# Get a line of text from the user
print "Please enter a sentance:\n";
@String = <stdin>;
print "Here is your sentance in Morse Code:\n"
# Convert each letter of the sentence to morse code and display it
foreach $Letter (@String) # Line 26
{
print "%MorseCode{$Letter} ";
}
print "\n";
This is what I get when I try and run it:
Hercules[50]% perl FirstProgram.pl
syntax error at FirstProgram.pl line 26, near "$Letter ("
Execution of FirstProgram.pl aborted due to compilation errors.
Anybody know what I'm doing wrong on line 26?
Also, while it won't prevent the program from running, I think you want {} around the hash initialization instead of (), in order to make it do what you want. I'm not even sure though, damn... forgetting my Perl because of learning Python :)
Well, my program runs now, but instead of printing the morse code part it just prints nothing. Here's what I have now:
#!/usr/bin/perl -w
# Build Morse Code Associtive Array
%MorseCode = ( "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" => "--..",
" " => "/");
# Get a line of text from the user
print "Please enter a sentance:\n";
$String = <stdin>;
print "Here is your sentance in Morse Code:\n";
# Convert each letter of the sentence to morse code and display it
foreach $Letter ($String)
{
print "$MorseCode{$Letter} ";
}
print "\n";
It is supposed to be grabbing one letter at a time from $String, and printing out the associated value of what's in the morse code array. I've also tried using %String in the foreach line, but it gives the same results. Anybody know why it's not printing the morse code? Thanks in advance, and thanks Telastyn for catching that missing semicolon.
** Edit: Thanked Zahlman instead of Telastyn **
[Edited by - deadlydog on October 2, 2004 4:03:02 PM]
#!/usr/bin/perl -w
# Build Morse Code Associtive Array
%MorseCode = ( "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" => "--..",
" " => "/");
# Get a line of text from the user
print "Please enter a sentance:\n";
$String = <stdin>;
print "Here is your sentance in Morse Code:\n";
# Convert each letter of the sentence to morse code and display it
foreach $Letter ($String)
{
print "$MorseCode{$Letter} ";
}
print "\n";
It is supposed to be grabbing one letter at a time from $String, and printing out the associated value of what's in the morse code array. I've also tried using %String in the foreach line, but it gives the same results. Anybody know why it's not printing the morse code? Thanks in advance, and thanks Telastyn for catching that missing semicolon.
** Edit: Thanked Zahlman instead of Telastyn **
[Edited by - deadlydog on October 2, 2004 4:03:02 PM]
foreach $Letter ($String){ print "$MorseCode{$Letter}";}
foreach doesn't iterate over the letters of the string, but rather over the one-element-list $string is.
Thermo/Konfu
Perl does not have a built in character type. If moris code is the only thing your program does, Python would probably be a better choice. If you choose to use perl, This should get you started.
try
By definition foreach requires a multiple variable type as its 2nd parameter. $string is a single variable. split(//,$variable) should iirc split the single variable into a character array.
foreach $letter (split (//,$string)){...}
By definition foreach requires a multiple variable type as its 2nd parameter. $string is a single variable. split(//,$variable) should iirc split the single variable into a character array.
Scalar vs Array context, people!
$string is the scalar value retrieved from stdin. @string is the same value, but evaluated/represented as an array of characters, which can be passed to iterative constructs.
$string is the scalar value retrieved from stdin. @string is the same value, but evaluated/represented as an array of characters, which can be passed to iterative constructs.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement