Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Unable to create HASH type

by anjultyagi (Novice)
on May 07, 2018 at 10:59 UTC ( #1214143=perlquestion: print w/replies, xml ) Need Help??
anjultyagi has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have some requirement, in which i have to use perl language in postgres DB. I have been facing one issue in creating the Hash. below is my code that is having issue:

while (($key,$value) = each(%{$parts_jsonlb->[$slb]})) { #elog(NOTICE, 'Key '.$key .' Value '.$value); #elog(NOTICE, 'REfernece '. ref($value) ); if (ref($value) eq 'HASH') {%l_pa = (%l_pa, $value);} else { elog(NOTICE, 'Key '.$key .' Value '.$value); my %perm = ($key => $value); elog(NOTICE, 'l_pa '.Dumper(%perm)); %l_pa = (%l_pa,%perm); } }

in the code "my %perm = ($key => $value);" i am not able to generate the HASH value. When I tried to dumper, it does not look like the HASH value. Current code output: NOTICE: l_pa $VAR1 = 'amountSubmitted'; $VAR2 = '0.00'; Expected output: 'amountSubmitted' => '0.00' its really urgent and i am stuck with that code.

Replies are listed 'Best First'.
Re: Unable to create HASH type
by roboticus (Chancellor) on May 07, 2018 at 11:10 UTC


    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.

Re: Unable to create HASH type
by haukex (Canon) on May 07, 2018 at 11:17 UTC

    What happens when you Dumper(%hash) is that %hash gets turned into a list of key/value pairs which are then passed to Data::Dumper as if you had written Dumper("key1","value1","key2","value2",...), and it then prints each individually. The best workaround is usually to write Dumper(\%hash) instead, since a reference to a hash won't get "flattened" this way.

    (Update before posting: roboticus beat me to it :-) )

    By the way, the code %l_pa = (%l_pa, $value); doesn't make much sense to me, but I'm not sure what its intention is because you haven't provided an SSCCE, perhaps you could do that so we can be of more help? Just a guess: perhaps you meant to dereference the hash reference with %$value?

    Minor edits.

Re: Unable to create HASH type
by johngg (Canon) on May 07, 2018 at 11:30 UTC

    Further to what roboticus said, you can use Data::Dumper->Dump() or its faster C-coded equivalent ->Dumpxs(), which are not exported by default, to obtain a perhaps clearer output. This is more useful if inspecting multiple variables in one go.

    johngg@abouriou ~/perl/Monks $ perl -Mstrict -Mwarnings -MData::Dumper + -E ' my %hash = ( Name => q{Fred}, Age => 28, Sex => q{M} ); print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( 'Sex' => 'M', 'Age' => 28, 'Name' => 'Fred' );

    I hope this is of interest.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1214143]
Approved by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-12-16 13:12 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (70 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!