Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

First Perl Program trouble

This topic is 5041 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
Advertisement
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
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!