line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::HashSum; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
@EXPORT_OK = qw(hashsum); |
4
|
|
|
|
|
|
|
$VERSION = 0.02; |
5
|
1
|
|
|
1
|
|
3555
|
use base 'Exporter'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
123
|
|
6
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
117
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# hashsum: Sum a list of key-value pairs on a per-key basis |
9
|
|
|
|
|
|
|
sub hashsum { |
10
|
1
|
|
|
1
|
0
|
1453
|
my %sum; |
11
|
1
|
|
|
|
|
7
|
while (@_) { |
12
|
4
|
|
|
|
|
5
|
my $key = shift; |
13
|
4
|
|
|
|
|
12
|
$sum{$key} += shift; |
14
|
|
|
|
|
|
|
} |
15
|
1
|
|
|
|
|
7
|
%sum; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Math::HashSum - Sum a list of key-value pairs on a per-key basis |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Math::HashSum qw(hashsum); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my %hash1 = (a=>.1, b=>.4); |
27
|
|
|
|
|
|
|
my %hash2 = (a=>.2, b=>.5); |
28
|
|
|
|
|
|
|
my %sum = hashsum(%hash1,%hash2); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
print "$sum{a}\n"; # Prints .3 |
31
|
|
|
|
|
|
|
print "$sum{b}\n"; # Prints .9 |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This module allows you to sum a list of key-value pairs on a per-key basis. |
36
|
|
|
|
|
|
|
It adds up all the values associated with each key in the given list and |
37
|
|
|
|
|
|
|
returns a hash containing the sum associated with each key. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The example in the synopsis should explain usage of the module effectively. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 AUTHORS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
David James |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SEE ALSO |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
L, L for an example of this module in use |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 LICENSE |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Copyright (c) 2002 David James |
52
|
|
|
|
|
|
|
All rights reserved. |
53
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
54
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |