I have the two hash of arrays (HoA) that correspond to the following file:
A 10 15 20 25
B 21 33 21 23
C 43 14 23 23
D 37 45 43 49
Here are my HoAs.
my %first_HoA = (
'A' => [ '10', '15', '20', '25'],
'B' => [ '21', '33', '21', '23'],
);
my %second_HoA = (
'A' => [ '10', '15', '20', '25'],
'B' => [ '21', '33', '21', '23'],
'C' => [ '43', '14', '23', '23'],
'D' => [ '37', '45', '43', '49'],
);
For every $key in the second HoA (A-D), I want to call a subroutine that does calculations on it's corresponding array and the array of every $key in the first HoA (A-B). Based on the calculations, the subroutine should return a key from the first HoA that yields the highest value. In other words, the subroutine should only be called for every $key in the second HoA and return the $key in the first HoA that yields the best value based on the calculations of the arrays of the keys in the first HoA.
Here's how I have it right now. Say I have an arbitrary subroutine called calculate
my $iterations = 1;
foreach my $key ( keys %second_HoA ) {
for my $arrayref (values %first_HoA){
calculate($first_HoA{$key}, $arrayref);
print "Iteration: $iterations\n";
$iterations++;
}
}
As you can see, this calls calculate
8 times. I only want to call calculate
for every $key in %second_HoA
which is 4 times but I also need to pass in the $arrayref
to do the calculations in the subroutine.
Does anyone know how I can do this?
Another way I was thinking of doing this was passing in a hash_ref of the first_HoA like so:
foreach my $key ( keys %second_HoA ) {
calculate($second_HoA{$key}, \%first_HoA);
print "Iteration: $iterations\n";
$iterations++;
}
Doing this calls calculate
4 times which is what I want but it complicates things in the subroutine.
Any suggestions. thanks.