Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Print values and keys from hash of hashes.

by Sami_R (Sexton)
on Feb 06, 2020 at 22:31 UTC ( [id://11112518]=perlquestion: print w/replies, xml ) Need Help??

Sami_R has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a hash and trying to get the keys and values, but not getting the expected output doing somewhere wrong

#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my %hash = { 'debt-1' => { 'debt_id' => 10, 'mobile' => 11, 'address1' => 12 }, 'dedt-2' => { 'debt_id' => 20, 'mobile' => 21, 'address1' => 22 }, 'debt-3' => { 'debt_id' => 30, 'mobile' => 31, 'address1' => 32 }, 'debt-4' => { 'debt_id' => 40, 'mobile' => 41, 'address1' => 42 } };
print "\n---------------------Top 4 ---------------------\n"; print Dumper \%hash; print "\n---------------------Top 5 ---------------------\n";

When I tried print the dump, i get

---------------------Top 4 --------------------- $VAR1 = { 'HASH(0x7fc9b5936d80)' => undef }; ---------------------Top 5 ---------------------

-------------2-------------

my $cnt = 1; while ( my ($name, $ITEM) = each %hash ){ print "[ $cnt ] --- $name \n"; # print Dumper $ITEM; my $in_cnt = 1; while ( my ($item, $value) = each %{$ITEM}) { print "[ $cnt ] --- [ $in_cnt ] --- $item => $value \n"; # print Dumper $item; $in_cnt++; } $cnt++; }

And when i tried to extract the keys and values, i get

[ 1 ] --- HASH(0x7fc9b5936d80) Argument "HASH(0x7fc9b5936d80)"

Please give me directions to get the keys and values from the above hash.

Thanks in advanced

Replies are listed 'Best First'.
Re: Print values and keys from hash of hashes.
by stevieb (Canon) on Feb 06, 2020 at 22:46 UTC

    The following hash creation isn't doing what you think it is. It's creating a hash with a hash reference as its only key, and with no value (value is assigned as undef).

    my %hash = { 'debt-1' => { 'debt_id' => 10, 'mobile' => 11, 'address1' => 12 }, 'dedt-2' => { 'debt_id' => 20, 'mobile' => 21, 'address1' => 22 }, };

    Instead of using braces (ie. {}) to create the hash which is used to create a hash reference to an anonymous hash, you need to use parens instead:

    my %hash = ( 'debt-1' => { 'debt_id' => 10, 'mobile' => 11, 'address1' => 12 }, 'dedt-2' => { 'debt_id' => 20, 'mobile' => 21, 'address1' => 22 }, );

    Things should begin to fall into place after you've fixed that.

      Like stevo said, it is also mentioned in perlreftut (Make rule 2)
      If you write just "[]", you get a new, empty anonymous array. If you write just "{}", you get a new, empty anonymous hash. my %hash = ( 'debt-1' => { 'debt_id' => 10, 'mobile' => 11, 'address1' => 12 }, ); my $hash_ref = { 'debt-1' => { 'debt_id' => 10, 'mobile' => 11, 'address1' => 12 }, };
Re: Print values and keys from hash of hashes.
by hippo (Bishop) on Feb 06, 2020 at 22:55 UTC

    When you run the first code sample in your post you will see this:

    $ ./11112518.pl Reference found where even-sized list expected at ./11112518.pl line 7 +.

    That message is displayed because you used warnings - which was the right thing to do. This is telling you that instead of assigning a hash to %hash (a hash is an even-sized list) you are instead assigning a reference. As stevieb pointed out that's because you have enclosed the right-hand side in braces instead of brackets.

    It is good that you used warnings but it's only useful if you pay attention to and try to understand the warnings which are printed. If you don't understand them, you can always use diagnostics for more detailed explanations of the potential problems.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found