in reply to Creating a Hash Syntax Error

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>); } }

Replies are listed 'Best First'.
Re^2: Creating a Hash Syntax Error
by ms238 (Initiate) on Jul 28, 2014 at 10:41 UTC
    Thanks, airtap, Laurent_R and 1s44c. I will have to start indenting better. I have noticed that in a lot of programs, the left curly bracket is on the line above the print command and the right curly bracket is below it--instead of all on the same line. I was wondering why it isn't on the same line--unless it makes it easier to see if you left off a left or right curly bracket. I appreciate everyone that gave me help on this.