Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to detect a hash container key from a referenced sub value

by FalseVinylShrub (Chaplain)
on Dec 03, 2009 at 05:58 UTC ( [id://810755]=note: print w/replies, xml ) Need Help??


in reply to How to detect a hash container key from a referenced sub value

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:

  • initialise $total in my method example.
  • Add missing operand to += in qtyTotal

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.

Replies are listed 'Best First'.
Re^2: How to detect a hash container key from a referenced sub value
by igoryonya (Pilgrim) on Dec 03, 2009 at 10:28 UTC

    First, I want to say, I've made some typos in my code. I've used '=' instead of '=>' in hashes and I've forgot to apply a value in the subroutine. I fixed it now.

    Anyway.

    The obvious, as you say, yes. That's how I usually do it. I, usually, create a sub, supply the values explicitly to it and get the result. I just wanted to see, if it's possible to do it implicitly, also, I need that total value in the variable, because I am going to sort it by qtyTotal in dec order, by using a sort function. If it's not going to be automatically updated, I figure that I have to create an other temp variable, every time I use that sort function. I just wanted to see, if could it be done in a more elegant way. Also, it will eliminate any bugs, that could be caused by forgetting of updating a qtyTotal in that variable.

    I guess, you are right about creating object instead of just using a regular variable.

    I was hesitant with that because, the only time, I've used objects is when I refer to other libraries or modules and what always stopped me was that, i might be wrong, the requirement to create a module, describing the object in the separate file instead of just ability to make an object in the same file as the script. :(
      what always stopped me was that, i might be wrong, the requirement to create a module, describing the object in the separate file instead of just ability to make an object in the same file as the script.

      You don't necessarily need separate files.  Separate packages, yes, but you can have more than one package in the same file (if you really want):

      #!/usr/bin/perl package Foo; sub new { my $class = shift; return bless { @_ }, $class; } # ... package Bar; sub new { my $class = shift; return bless { @_ }, $class; } # ... package main; # the actual script my $foo = Foo->new( myattr => "foo" ); # create an obj use Data::Dumper; print Dumper $foo; __END__ $VAR1 = bless( { 'myattr' => 'foo' }, 'Foo' );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://810755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found