To add to the already great comments made...

When you initialize a hash it is standard convention to use '=>' between key/value pairs rather than commas...

e.g. {'P' =>'a','N' =>'b','U' =>'c'}

it is strictly a readability thing, but since code is written for people, readability is everything

Since a hash is just a "special" array, it may be easier to understand the problem by looking at an array example.

If you initialiaze an array with square brackets you get a reference to the array. On the other hand, if you initialize with parentheses

@x = ('P','a','N','b','U','c');

you will get the actual array.

Creating the hash with the {} braces works like the square brackets on an array.

Another great comment made was to suggest you should enable warnings on your code. I cannot agree more. You should just get in the automatic habit of adding strict and warnings to everything you write. e.g.

#!/usr/bin/perl
use strict;
use warnings;

my 2 bits...

Anyone know how to get square brackets to show up in my source listing on the perl monks site?

In reply to Re: Hash problem: each() failure by Anonymous Monk
in thread Hash problem: each() failure by Anonymous Monk

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.