in reply to hash problems

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

Replies are listed 'Best First'.
Re^2: hash problems
by weglarz (Novice) on Sep 20, 2010 at 17:29 UTC

    I actually have a couple more questions. For one, what does the => operator do? Also, could you please explain these lines a bit(specifically "each %passwords"):

    while (my ($key,$value) = each %passwords) { print "$key => $value\n";

    Thanks again.

      A very handy skill in learning Perl is gaining familiarity with documentation. I personally use the Perl documentation hosted on http://perl.org with associated hook-up to my browser search box to keep it immediately on hand. There's also the perldoc command-line utility, ActiveState bundles html-ified docs with its version, and I'm sure a plethora of other options.

      For operators, there's perlop. Within that document, the => operator is documented under Comma Operator. It is identical to a comma, except to transforms a bareword left-hand argument to a string. Specifically,

      The => operator is a synonym for the comma except that it causes its left operand to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores.
      Thus it allows:

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

      to be written as

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

      which I find to be more intuitive.

      The each function has a bit of magic in it. When called with a hash, it will set an iterator in the hash and returns a key, value pair. Each subsequent call (assuming you don't use another function that sets an iterator) returns another key/value pair until they are exhausted, at which time it will return and empty list or undef depending on context. Thus, a while(each) will systematically traverse a hash. On each iteration, I use list assignment to store the key/value pair and then output them in an interpolated string. The examples in each are likely more clear than any explanation I can give.