Reputation: 35
I'm currently working on some code written by the previous internship student. I'm not familiar to Perl so I have some problems in understanding what his code actually do. So it looks like:
$Hash{Key1}{Key2}++;
The original code was:
$genotypes_parent2_array{$real_genotype}{$individu_depth}++;
I use to see hashes in this form $Hash{Key} in order to get the value but I struggle with this one. Any help out there ? Thanks!
Upvotes: 0
Views: 82
Reputation: 69460
%Hash
is a hash of hashes.
That codes add 1
to the value of $Hash{Key1}{Key2}
which is the value of a hash element.
Upvotes: -1
Reputation: 69314
%genotypes_parent2_array
is a hash (so that's not a very good name for the variable!) Each value in the hash is a hash reference. So effectively you have a hash of hashes.
$genotypes_parent2_array{$real_genotype}
looks up the key $real_genotype
in the hash. And that value is (as we said above) a hash reference. If you have a hash reference, then you can look up values in the referenced hash using an arrow. So we can get to a value in the second-level hash using code like this:
$genotypes_parent2_array{$real_genotype}->{$individu_depth}
However, Perl has a nice piece of syntactic sugar. When you have two pairs of "look-up brackets" next to each other (as we have here) you can omit the arrow. So you can get exactly the same effect with:
$genotypes_parent2_array{$real_genotype}{$individu_depth}
And that's what we have here. We look up the key $real_genotype
in the hash %genotypes_parent2_array
. This gives us a hash reference. We then look up the key $individu_depth
in the referenced array and that gives us the value in the second-level hash. Your code then increments that value.
The manual page perldoc perldsc is a good introduction to using references to build complex data structures in Perl. In addition, I find Data::Dumper very useful for showing what a complex data structure looks like.
Upvotes: 4