Sign in to follow this  

First Perl Program trouble

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
try

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.

Share this post


Link to post
Share on other sites

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this