Question

Does anyone know how to dereference a hash of hashes so that I can use it in my subroutine. As you can see, I'm having trouble accessing my Hash of Hashes data structure in my subroutine.

my $HoH_ref = \%HoH;     # reference the hash of hashes

for(@sorted) {
print join("\t", $_, get_num($_, $HoH_ref))
}

sub get_num {
    my ($foo) = shift;
    my $HoH_ref = shift;
    my %HoH = %{$HoH_ref};    # dereference the hash of hashes
    my $variable = %HoH{$foo}{'name'};
    # do stuff
    return;
    }

I'm getting an syntax error on the second to last line %HoH{$protein}{'degree'} near %HoH{ and the hash of hashes is not recognizing $protein key from %HoH. I'm getting the error message: Global symbol "$protein" requires explicit package name. thanks

Was it helpful?

Solution

The syntax to access a hash element is $hash{KEY}, not %hash{KEY}.

my %HoH = %{$HoH_ref};
my $variable = $HoH{$foo}{name};
               ^
               |

But copying the entire hash is silly. Use

my $variable = $HoH_ref->{$foo}{name};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top