Sign in to follow this  
deadlydog

First Perl Program trouble

Recommended Posts

deadlydog    170
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
Zahlman    1682
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
deadlydog    170
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
demonkoryu    980

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
Telastyn    3777
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
Oluseyi    2103
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

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