Hello, I've stumbled upon an implementation problem in my script. Let's say I have a hash of phrases as keys and values as referenced unonymous hashes of quantities. There are several different types of quantities. For example: qty, qtySum, qtyCont, etc. Different keys may have different qty types. In other words, not all qty types exist in each key:
my %phrases = ( "phrase 1"=>{ qty=>1, qtySum=>5, qtyCont=>8 }, "phrase 2"=>{ qty=>10, qtyCont=>34 }, "phrase 3"=>{qty=>1} );
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, 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; }
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:
print "$phrases{'phrase 2'}->{'qtyTotal'}\n";
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.

Thanx 4 all help.


In reply to How to detect a hash container key from a referenced sub value by igoryonya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.