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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |