/[cvs]/nfo/perl/libs/Tie/SummableHash.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Tie/SummableHash.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Thu Feb 20 21:38:08 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
+ refactored code from misc::HashExt

1 joko 1.1 ## -------------------------------------------------------------------------
2     ## $Id: Exception.pm,v 1.2 2003/02/20 20:53:55 joko Exp $
3     ## -------------------------------------------------------------------------
4     ## $Log: Exception.pm,v $
5     ## -------------------------------------------------------------------------
6    
7    
8     package Tie::SummableHash;
9    
10     use strict;
11     use warnings;
12    
13     use base qw( Hash::Serializer );
14    
15     use overload
16     '+=' => \&sum_hash,
17     ;
18    
19     use Tie::IxHash;
20     use Data::Dumper;
21    
22    
23     sub new {
24     #print "new: ", __PACKAGE__, "\n";
25     my $class = shift;
26     my $value = shift;
27     tie(%{$value}, "Tie::IxHash");
28     return bless \$value => $class;
29     }
30    
31    
32     sub sum_hash {
33    
34     #print Dumper(@_);
35     my ($x, $y) = @_;
36     #print "x: ";
37     #print Dumper($x);
38     #print "y: ";
39     #print Dumper($y);
40     my $value = $$x;
41    
42     #$value->{sumCount}++;
43     foreach my $key (keys %{$y}) {
44     # initialize key if undefined yet to prevent "Use of uninitialized value in addition (+) at [...]"
45     $value->{$key} ||= 0;
46     # sum up statistic-counts
47     $value->{$key} += $y->{$key};
48     }
49    
50     return bless \$value => ref($x);
51     }
52    
53     1;
54     __END__

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed