Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Unable to create HASH type

by roboticus (Chancellor)
on May 07, 2018 at 11:10 UTC ( #1214146=note: print w/replies, xml ) Need Help??


in reply to Unable to create HASH type

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214146]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2019-10-14 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?