There are places in the program that need totals of those quantities. So, the first thing that I did, was iterated through an entire hash and added a qtyTotal with the sum of all of the qtys for each phrase to each key's anonymous referenced hash. Then, while writing a program, I've found that each qty value can change, which requires for the qtyTotal to be recalculated, so I descided to create a subroutine that I will use as referenced subroutine in qtyTotal instead of a statically calculated value. That subroutine will calculate all of the qty's dynamically when the qtyTotal is called. At first, it seemed all rainbowy until I found out that the subroutine needs to know which key's qty values need to be calculated:my %phrases = ( "phrase 1"=>{ qty=>1, qtySum=>5, qtyCont=>8 }, "phrase 2"=>{ qty=>10, qtyCont=>34 }, "phrase 3"=>{qty=>1} );
So, the question, as I mentioned above and as you saw in the sub, how does the subroutine know it's container hash key, in order to calculate the right key's qty values? I want to be able to get calculated result when I say something like:my %phrases = ( "phrase 1"=>{ qty=>1, qtySum=>5, qtyCont=>8, qtyTotal=>\&qtyTotal }, "phrase 2"=>{ qty=>10, qtyCont=>34, qtyTotal=>\&qtyTotal }, "phrase 3"=>{ qty=>1, qtyTotal=>\&qtyTotal } ); sub qtyTotal{ my $qty = 0; my $key = #I don't know what to use my $phObj = $phrases{$key}; foreach my $qType ('qty', 'qtySum', 'qtyCont'){ $qty += $phrases{$key}->{$qType} if exists $phrases{$key}->{$qType}; } return $qty; }
In this example's case, the result should be: 44 , since the subroutine knew that it was called from the container with 'phrase 2' key.print "$phrases{'phrase 2'}->{'qtyTotal'}\n";
Thanx 4 all help.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |