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

Hi all! I have a hash question for you.
Is it possible to add to the value of a hash...

Explaination:
$hash{$timestring}++;
If 15:00:00 was put in the hash twice the value for that key would be 2.

Now, is it possible to instead make something like this
$hash{$timestring}+$value;
so if the value 100 was associated with 15:00:00 the value would become 200 the next time 15:00:00 was put into the hash.
Any ideas on how to accomplish this.
Thanks!.
Heffa K.

Replies are listed 'Best First'.
Re: Tricky hash value
by bent (Scribe) on Apr 20, 2001 at 04:06 UTC
    Hi,
    How about:
    $hash{$timestring} += $value;
    bent
Re: Tricky hash value
by cLive ;-) (Prior) on Apr 20, 2001 at 04:17 UTC
    what about this:
    # sample related values my %val = ('14:00:00' => '200', '15:00:00' => '100', '16:00:00' => '300'); my @time = ('15:00:00','14:00:00','15:00:00','16:00:00'); my %hash; for (@time) { $hash{$_} += $val{$_}; }

    Or am I misunderstanding the question?

    cLive ;-)

Re: Tricky hash value
by Anonymous Monk on Apr 20, 2001 at 04:16 UTC
    Thanks! That was exactly what I was looking for.
    Thanks again.
    HeffaK