Welcome to the monastery, and welcome back to coding.

You are doing your assignment incorrectly.

%passwords = {"Matt", "s1k1d52", "scuzzy", "2ab928", "Marky", "s8291s", "Jeb", "jeb23"};

should read

%passwords = ("Matt", "s1k1d52", "scuzzy", "2ab928", "Marky", "s8291s", "Jeb", "jeb23");

Parentheses specify a list, where as curly brackets in a variable assignment specify a hash reference (See perlreftut). A reference is a scalar that points to a hash, in the same vein as (but different than) a pointer. A read through of perldata might clarify the nature of variable types in Perl.

Are you working from a book? I ask because there are a few stylistic issues that may hinder your learning. Of course, all of this is subjective.

  1. Especially coming from a strongly typed background like Java, you will probably want to use strict and warnings. See Use strict warnings and diagnostics or die for a good discussion of what it can do for you.
  2. On line 9, you have a problem with interpolation because you have used double-quotes for both your key stringification and for wrapping your output text. Easiest in this case would be just skipping the quotes around your key - Perl will automatically convert things that look like keys into strings for hash access.
  3. You'll need to explicitly include newlines in your print statements if you don't want them to run together. A double-quoted escaped n ("\n") does the trick.
  4. The contents of a hash will not interpolate in a double-quoted context. You'll need to more explictly format your output.

The version of your script that I would write would look more like:

#!usr/bin/local/perl use strict; use warnings; my %passwords = (Matt => "s1k1d52", scuzzy => "2ab928", Marky => "s8291s", Jeb => "jeb23", ); delete $passwords{jeb}; if(exists $passwords{Matt}){ print("$passwords{Matt} is my password.\n"); } if(defined $passwords{"scuzzy"}){ print("$passwords{scuzzy} IS defined!\n"); } print "\nEveryone's passwords:\n"; while (my ($key,$value) = each %passwords) { print "$key => $value\n"; }

In reply to Re: hash problems by kennethk
in thread hash problems by weglarz

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.