Indent your code, it makes it a lot more readable and will help you to find errors. Use warnings and strict. Declare all variables. The fat arrow '=>' makes hash assignments more readable. Chomp assignments to save a line.
There was nothing wrong with what you posted except the shebang line. A bit of polish makes it a lot more maintainable.
Here is a cleaned up version:
#!/usr/bin/perl use warnings; use strict; my %words = ( Sarah => 'cat', Bob => 'dog', Ronald => 'apple', Eric => 'banana' ); print "What is your name?"; chomp (my $name = <STDIN>); if ($name eq "Jerry") { print "Hi, Jerry! Have a good day!\n"; } else { print "Hello, $name!\n"; #normal greeting my $secretword = $words{$name}; #get the secret word print "Guess the secret word.\n"; chomp (my $guess = <STDIN>); while ($guess ne $secretword) { print "I'm sorry. That is not the correct word. Please try aga +in.\nWhat is the secret word?"; chomp ($guess = <STDIN>); } }
In reply to Re: Creating a Hash Syntax Error
by 1s44c
in thread Creating a Hash Syntax Error
by ms238
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |