Date: Sat, 30 Jun 2001 19:27:11 -0700
From: "David Frauzel" <nogard@gnosrehtaew.ten>
Subject: inline average
Message-Id: <9hm1cs$2h15$1@news.aros.net>

This is perhaps esoteric, but I'm curious.

Let's say I have the hash:

%values = (
  "red" => 5.0,
  "orange" => 4.0,
  "yellow" => 5.5,
  ...
)

I'd like to have a final element in the hash, "average", which always points
to the average of all of the other elements - something like an inline
function. I'm thinking it would look like this:

$values{"average"} = {
  my $total = 0;
  foreach $color (@spectrum) {
    $total += $values{$color};
  }
  $total = $total / ($#spectrum + 1);
  return $total;
}

(The above is obviously not real perl, it's just for the sake of
illustration.)

Given that, I could then say:

$values{"red"} = 6.0;

and immediately ask for:

print $values{"average"};

to get an updated average of red, orange, and yellow.

Is this possible?

David Frauzel





