in reply to Re: hash problems
in thread hash problems

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.

Replies are listed 'Best First'.
Re^3: hash problems
by kennethk (Abbot) on Sep 20, 2010 at 17:43 UTC
    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.

Re^3: hash problems
by Anonymous Monk on Sep 20, 2010 at 17:36 UTC