line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package List::Categorize; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# ABSTRACT: Categorize list items into a hash of named sublists. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# See documentation after __END__ below. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
112891
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
118
|
|
9
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
98
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
## Module Interface |
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
3
|
|
17
|
use base 'Exporter'; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
837
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
16
|
|
|
|
|
|
|
our @EXPORT = qw(); |
17
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
18
|
|
|
|
|
|
|
categorize |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
21
|
|
|
|
|
|
|
all => \@EXPORT_OK |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
## Subroutines |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub categorize (&@) |
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# Usage: %hash = categorize {BLOCK} @LIST |
30
|
|
|
|
|
|
|
# Returns: a hash of lists |
31
|
|
|
|
|
|
|
# |
32
|
|
|
|
|
|
|
# Creates a hash by running a subroutine for each element in a list. The |
33
|
|
|
|
|
|
|
# subroutine returns a hash key (the "category") for the current |
34
|
|
|
|
|
|
|
# element. (If the subroutine returns undef for a list element, that |
35
|
|
|
|
|
|
|
# element is not placed in the resulting hash.) |
36
|
|
|
|
|
|
|
# |
37
|
|
|
|
|
|
|
# The resulting hash contains a key for each category, and each key |
38
|
|
|
|
|
|
|
# refers to a list of the elements that correspond to that category. |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
{ |
41
|
|
|
|
|
|
|
# Parameters |
42
|
|
|
|
|
|
|
# |
43
|
12
|
|
|
12
|
1
|
150646
|
my $coderef = shift; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# @_ is used directly, in the loop below. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# This is the hash of lists that will be returned to the caller. |
48
|
|
|
|
|
|
|
# |
49
|
12
|
|
|
|
|
30
|
my %sublists = (); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Iterate over the provided list, categorizing |
52
|
|
|
|
|
|
|
# each element. |
53
|
|
|
|
|
|
|
# |
54
|
12
|
|
|
|
|
27
|
for my $element (@_) |
55
|
|
|
|
|
|
|
{ |
56
|
|
|
|
|
|
|
# Localize $_, then copy the current element into it, so the |
57
|
|
|
|
|
|
|
# categorizer subroutine can refer to $_ (in the same way as |
58
|
|
|
|
|
|
|
# map, grep, and sort do). |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# Copying the element keeps it from acting as an alias into the |
61
|
|
|
|
|
|
|
# @_ list, so the categorizer can modify $_ without damaging the |
62
|
|
|
|
|
|
|
# source list. |
63
|
|
|
|
|
|
|
# |
64
|
174
|
|
|
|
|
195
|
local $_ = $element; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Execute the categorizer subroutine to determine the category |
67
|
|
|
|
|
|
|
# for this element. |
68
|
|
|
|
|
|
|
# |
69
|
174
|
|
|
|
|
311
|
my $category = $coderef->(); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# If a category was returned, use it as a key in the %sublists |
72
|
|
|
|
|
|
|
# hash, and add the current element to the list referenced by |
73
|
|
|
|
|
|
|
# that key. |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# If the categorizer didn't return a value (or returned undef), |
76
|
|
|
|
|
|
|
# then leave this element out of %sublists entirely. |
77
|
|
|
|
|
|
|
# |
78
|
174
|
100
|
|
|
|
1171
|
if (defined $category) |
79
|
|
|
|
|
|
|
{ |
80
|
155
|
|
|
|
|
162
|
push @{ $sublists{ $category } }, $_; |
|
155
|
|
|
|
|
410
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
12
|
|
|
|
|
90
|
return %sublists; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |