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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.