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.
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 | |
by kennethk (Abbot) on Sep 20, 2010 at 17:43 UTC | |
by Anonymous Monk on Sep 20, 2010 at 17:36 UTC |