Reputation: 2358
I have seven hashes that are subsets of the hash, %ALL.
%ALL looks like this:
ID# => ID# Date Measure1 Measure2 Measure3 ... Measure6
ID# => ID# Date Measure1 Measure2 Measure3 ... Measure6
ID# => ID# Date Measure1 Measure2 Measure3 ... Measure6
All seven subset hashes look like this:
ID# => Measure1
ID# => Measure1
ID# => Measure1
I want to systematically check every combination of two or three hashes and list all the hits for each combination. I know there are modules on CPAN for this kind of combinations analysis but I would like to come up with a pure scripting solution rather than install new software.
I tried writing the following function and calling it with two hash references:
ComPair(\%ATG12, \%ATG5);
sub ComPair {
my $ref = shift;
my $chek = shift;
while ( my ( $key, $value ) = each($ref) ) {
if ( exists $chek{$key} ) { print $ALL{$key} }
}
}
and got the following error message.
Global symbol "%chek" requires explicit package name at Combo_Ranks2.pl line 41.
I figure that if I can get this pair-comparing function to work, I can design some kind of loop to run it on every pair of hashes and that applying the same approach for triplet comparisons will be only slightly more complicated.
For right now though, I need to figure out how to make my subroutine work. My best guess is that it does not recognize the second hash I pass to it as a hash but I'm not even sure that is the case. Any ideas?
Upvotes: 0
Views: 1045
Reputation: 57470
"$chek{$key}
" tells Perl to look for a hash named %chek
, but you want to use a scalar reference to a hash, $chek
. This can be done by writing either $chek->{$key}
or $$chek{$key}
.
Upvotes: 3