-1

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.

cooldood3490
  • 2,418
  • 7
  • 51
  • 66
  • please post the subroutine code – amphibient Mar 23 '13 at 23:00
  • the subroutine's pretty long and involved but I'll try to post a minimal representation of it. – cooldood3490 Mar 23 '13 at 23:06
  • okay I just posted the subroutine – cooldood3490 Mar 23 '13 at 23:21
  • What are the 4 calls suppose to be? Fill in the blanks: `calculate([ '10', '15', '20', '25'], ???)`, `calculate([ '21', '33', '21', '23'], ???)`, ... – ikegami Mar 23 '13 at 23:57
  • I'm taking the second approach now. The calls are `calculate(['10', '15', '20', '25'], \%first_HoA)`, `calculate([ '21', '33', '21', '23'], \%first_HoA)`, `calculate([ '43', '14', '23', '23'], \%first_HoA)`, and `calculate([ '37', '45', '43', '49'], \%first_HoA)` – cooldood3490 Mar 24 '13 at 00:02

1 Answers1

1

You say calculate($second_HoA{$key}, \%first_HoA) "complicates things", but I don't see how that's possible. It seems to me it's the minimum of information you need, and it's in a convenient format.

Anything less would complicate things, in the sense that you wouldn't have the information you need to do your calculations.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • yeah, I'm going to try that approach. Once I'm in the subroutine I can deference the hash `%first_HoA` then I can loop through the values (elements of the array) of the hash and do the calculations. – cooldood3490 Mar 24 '13 at 00:07