line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 Name |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
qbit::Array - Functions to manipulate arrays. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package qbit::Array; |
8
|
|
|
|
|
|
|
$qbit::Array::VERSION = '2.3'; |
9
|
8
|
|
|
8
|
|
38
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
272
|
|
10
|
8
|
|
|
8
|
|
37
|
use warnings; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
225
|
|
11
|
8
|
|
|
8
|
|
34
|
use utf8; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
37
|
|
12
|
|
|
|
|
|
|
|
13
|
8
|
|
|
8
|
|
218
|
use base qw(Exporter); |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
1025
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
8
|
|
|
8
|
|
15
|
our (@EXPORT, @EXPORT_OK); |
17
|
|
|
|
|
|
|
|
18
|
8
|
|
|
|
|
24
|
@EXPORT = qw( |
19
|
|
|
|
|
|
|
in_array arrays_intersection arrays_difference array_uniq |
20
|
|
|
|
|
|
|
array_n_min array_min array_n_max array_max |
21
|
|
|
|
|
|
|
array_avg |
22
|
|
|
|
|
|
|
); |
23
|
8
|
|
|
|
|
5113
|
@EXPORT_OK = @EXPORT; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 Functions |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head2 in_array |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
B |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=over |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=item |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
B<$elem> - scalar; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
B<$array> - array ref. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=back |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
B boolean. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub in_array($$) { |
49
|
0
|
|
|
0
|
1
|
|
my ($elem, $array) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my %hs; |
52
|
0
|
|
|
|
|
|
@hs{@$array} = (); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return exists $hs{$elem}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 arrays_intersection |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
B |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
B<$array_ref1>; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
B<$array_ref2>; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
B<...>; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
B<$array_refN>. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=back |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
B array ref, intersection of all arrays (unique values). |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub arrays_intersection(@) { |
86
|
0
|
|
|
0
|
1
|
|
my %hs = (); |
87
|
0
|
|
|
|
|
|
foreach my $array (map {array_uniq($_)} @_) { |
|
0
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
exists($hs{$_}) ? ($hs{$_}++) : ($hs{$_} = 1) for @$array; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return [grep {$hs{$_} == @_} keys %hs]; |
|
0
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 arrays_difference |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
B |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=over |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
B<$array1> - array ref, minuend; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
B<$array2> - array ref, subtrahend. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=back |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
B array ref. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub arrays_difference($$) { |
115
|
0
|
|
|
0
|
1
|
|
my ($array1, $array2) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my %hs; |
118
|
0
|
|
|
|
|
|
@hs{@$array2} = (); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return [grep {!exists($hs{$_})} @$array1]; |
|
0
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 array_uniq |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
B |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=over |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
B<@array> - each element may be array ref or scalar. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=back |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
B array ref, unique values from all arrays. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub array_uniq(@) { |
140
|
0
|
|
|
0
|
1
|
|
my %hs; |
141
|
0
|
0
|
|
|
|
|
@hs{ref($_) eq 'ARRAY' ? grep {defined($_)} @$_ : ($_)} = () for @_; |
|
0
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return [keys %hs]; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 array_n_min |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
B |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
B<@array> - array of numbers. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
B number, min value (numeric comparasion). |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub array_n_min(@) { |
162
|
0
|
|
|
0
|
1
|
|
my $min = $_[0]; |
163
|
0
|
|
|
|
|
|
foreach (@_) { |
164
|
0
|
0
|
|
|
|
|
$min = $_ if $min > $_; |
165
|
|
|
|
|
|
|
} |
166
|
0
|
|
|
|
|
|
return $min; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 array_min |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
B |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=over |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
B<@array> - array of strings. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=back |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
B string, min value (string comparasion). |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub array_min(@) { |
186
|
0
|
|
|
0
|
1
|
|
my $min = $_[0]; |
187
|
0
|
|
|
|
|
|
foreach (@_) { |
188
|
0
|
0
|
|
|
|
|
$min = $_ if $min gt $_; |
189
|
|
|
|
|
|
|
} |
190
|
0
|
|
|
|
|
|
return $min; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 array_n_max |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
B |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=over |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
B<@array> - array of numbers. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=back |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
B number, max value (numeric comparasion). |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub array_n_max(@) { |
210
|
0
|
|
|
0
|
1
|
|
my $max = $_[0]; |
211
|
0
|
|
|
|
|
|
foreach (@_) { |
212
|
0
|
0
|
|
|
|
|
$max = $_ if $max < $_; |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
|
|
|
return $max; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 array_max |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
B |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=over |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
B<@array> - array of strings. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=back |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
B string, max value (string comparasion). |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub array_max(@) { |
234
|
0
|
|
|
0
|
1
|
|
my $max = $_[0]; |
235
|
0
|
|
|
|
|
|
foreach (@_) { |
236
|
0
|
0
|
|
|
|
|
$max = $_ if $max lt $_; |
237
|
|
|
|
|
|
|
} |
238
|
0
|
|
|
|
|
|
return $max; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 array_avg |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
B |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=over |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
B<@array> - array of numbers. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=back |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
B number, average value. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub array_avg(@) { |
258
|
0
|
|
|
0
|
1
|
|
my $sum = 0; |
259
|
0
|
|
|
|
|
|
$sum += $_ foreach @_; |
260
|
0
|
|
|
|
|
|
return $sum / @_; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
1; |