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

Dear Monks,

Please look at the following code

use strict; use warnings; use Data::Dumper; open (S,"<","sample.data") or die $!; my @file=<S>; my %samplehash; my @fields; my ($key,$subkey); for ( my $i=0;$i<=$#file;$i++) { chomp $file[$i]; @fields=split /\s+/,$file[$i]; $samplehash{$fields[1]}{$fields[2]}=$fields[0]; } foreach $key (keys %samplehash) { print "\n $key = ". $samplehash{$key}; foreach $subkey (keys %{$samplehash{$key}}) { print "\t$subkey = $samplehash{$key}{$subkey} "; } } close S;

This is the input file

cow NM_178575.2 0 cow NM_181451.1 1 cow NM_001038581.1 2 cow NM_001037618.1 3 cow NM_001034757.1 4 cow NM_001076304.1 5 cow NM_001076305.1 6 cow NM_001076273.1 7 cow NM_001076486.1 8 cow NM_001076951.1 9 cow NM_001077070.1 10 cow NM_001077006.1 11 cow NM_001078104.1 12 cow NM_001078077.1 13 cow NM_001077830.1 14 cow NM_001079784.1 15 cow NM_001078135.1 16 cow NM_001080323.1 17 cow NM_001080346.1 18 cow NM_001080347.1 19 cow NM_174222.2 20 cow NM_001080364.1 21

When i try to print the key of the first hash table, using print "\n $key = ". $samplehash{$key};, i see the output something very weird as follows :

NM_001077006.1 = HASH(0x224be2c) 11 = cow

Can someone tell me the problem with my print statement, please. I didn't understand what is the HASH(0x224be2c) ,when i am expecting it to print 11

Replies are listed 'Best First'.
Re: Print the key of the first hash table in hash of hashes
by perl_lover (Chaplain) on Nov 30, 2010 at 06:22 UTC
    You are trying to print a hash ref there. In the nested hash structure $samplehash{$key} will return a hash ref not a scalar value. Change the print to print "\n $key = ". Dumper($samplehash{$key});, you will understand what I am talking about.
Re: Print the key of the first hash table in hash of hashes
by kcott (Archbishop) on Nov 30, 2010 at 06:33 UTC

    $samplehash{$key} is a hashref. You're accessing its keys with $samplehash{$key}{$subkey}.

    I'd recommend using Data::Dumper to see the entire contents and structure of %samplehash. I suspect that when you visualise the whole data structure, the reasons for your output will become apparent.

    -- Ken

Re: Print the key of the first hash table in hash of hashes
by ganeshwari (Sexton) on Nov 30, 2010 at 09:49 UTC
    As told in above replies, you're trying to print the hash ref, if you want to be printed as numeric values ( hash key value 11 or 12 or ) use the print statement as, print "\n $key = ", ( keys %{$samplehash{$key}});
    Ganeshwari
Re: Print the key of the first hash table in hash of hashes
by roboticus (Chancellor) on Nov 30, 2010 at 12:51 UTC

    sundeep:

    By your posts title, you want to print the key of the "first" entry in the referenced hash. There's really no "first", as hashes aren't stored in any particular order. But if that's really what you want, you should be able to do it like:

    my ($first_key) = keys %{$samplehash{$key}}; print "\n $key = " . $first_key;

    However, I think I'd simply show the hash structure by replacing the print statement with:

    foreach $key (keys %samplehash) { print "\n$key = {\n"; foreach $subkey (keys %{$samplehash{$key}}) { print "\t$subkey = $samplehash{$key}{$subkey}"; } print "\n}"; }

    and then adding (after the end

    ...roboticus

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