I just started trying complex structures in Perl, and Bi-Dimensional arrays and hashes.

I was making this form processor, and to be able to allow my co-workers (who have little idea about perl) to add form fields in the program without worrying about updating their use later in the program, so I hacked this piece of code:

%fields = ('1.Name' => $cgi->param('name') || 'Anonymous', '2.Email' => $cgi->param('email') || 'No email Supplied', '3.Comments' => $cgi->param('comments') || 'No Comments', '4.Satisfied' => $cgi->param('radio1') || 'Not Specified', );
In order to use later in the program
foreach (sort keys %names) { print "$_ : $names{$_} \n"; }

Why would using the %fields hash in a hash context

not work, and instead, using it in an array context works??

Is this always the way with hashes and references?? ( I might not be using the proper namings here ), or maybe it's the way I'm dereferencing the variable later??

Finally the code worked, but I need to get a grip of what happened, cuz I fixed it by mistake

Thanx


Chady | http://chady.net/

In reply to Hashes and Refs by Chady

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.