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

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


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 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 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 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.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214146]
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
Find Nodes?
    Voting Booth?