anjultyagi:

You're creating the hash value just fine. It's just that when you pass a hash to a function, it's expanding into a list. So when Dumper runs, it's dumping each value in the list separately, so it doesn't look like what you're expecting. Here's an example:

$ cat u.pl use strict; use warnings; use Data::Dumper; my $hashRef = {amountSubmitted=>1}; print "a hash reference: ", Dumper($hashRef), "\n"; my %hash = (amountSubmitted=>0); print "a hash expands to a list: ", Dumper(%hash), "\n"; print "pass the hash as a ref: ", Dumper(\%hash), "\n";

When run, it'll look like this:

$ perl u.pl a hash reference: $VAR1 = { 'amountSubmitted' => 1 }; a hash expands to a list: $VAR1 = 'amountSubmitted'; $VAR2 = 0; pass the hash as a ref: $VAR1 = { 'amountSubmitted' => 0 };

So when you're going to use Dumper to display a hash, be sure to use a hash reference instead of letting the hash expand to a list of values.

You should be aware that the same thing happens with arrays: If you use Dumper on an array, you'll get $VAR=first array value, $VAR=second array value, etc., unless you use an array reference:

$ perl u.pl array reference: $VAR1 = [ 'first value', 'second value' ]; array: $VAR1 = 'first value'; $VAR2 = 'second value'; array as ref: $VAR1 = [ 'first value', 'second value' ];

Update: Added bit about array/arrayref.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Unable to create HASH type by roboticus
in thread Unable to create HASH type by anjultyagi

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.