replicant4 has asked for the wisdom of the Perl Monks concerning the following question:

HI it's me again Being a beginner with perl at best, I have another question for you, after my problem with the pairwise comparisons I have a hash which holds a key with two letters, and then a numerical value. How can I access these values in order to perform calculations on them. I need to log them and then add them. I have written a small subroutine, that calculates the log with base 2 so I first need to use that (call it log_2) , to each numerical value in my hash and then add these values. Once again thanks in advance for anyone's help
  • Comment on Accessing and performing calculations with elements of a hash

Replies are listed 'Best First'.
Re: Accessing and performing calculations with elements of a hash
by si_lence (Deacon) on Oct 21, 2004 at 12:34 UTC
    Hi,
    If I get that right, you want to sum over all values in your hash, right?
    If so you can use something like this:
    my %h=(ab=>"12", ac=>"14", ad=>"45"); my $sum; foreach (values(%h)) { my $log2 = &log_2($_); $sum +=$log2; } print "$sum \n";
    si_lence
Re: Accessing and performing calculations with elements of a hash
by Random_Walk (Prior) on Oct 21, 2004 at 14:55 UTC
    $sum += log_2($_) for values %hash

    Cheers,
    R.