Hi

Forgive me if I've missed something obvious, but why don't you just create a subroutine that accepts a hash and calculates the sum:

# # Example code, not tested at all! # my %phrases = ( "phrase 1"=>{ qty=1, qtySum=5, qtyCont=8, qtyTotal=\&qtyTotal }, # ... ); sub qtyTotal{ my $qty = 0; my $phrase = shift; foreach my $qType ('qty', 'qtySum', 'qtyCont'){ $qty += $phrases{$qType} if exists $phrases{$qType}; } return $qty; } # and call like this: my $total = qtyTotal($phrases{'phrase 1'});

Seems like you're trying to do something in a somewhat object-oriented way. If you want to do that, you should look into making actual objects which comes with mechanisms for the method to know which object it was called on:

# incomplete example, not tested sub total { my $self = shift; my $total = 0; foreach my $key (qw{blah blah blah}) { $total += $self->{$key} } return $total; } print $phrase->total();

If that looks like what you're trying to do, you need to read up on Perl OO... I suggest you post again if you need pointers on where to start.

Edits:

HTH

FalseVinylShrub

N.B. Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.


In reply to Re: How to detect a hash container key from a referenced sub value by FalseVinylShrub
in thread 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.