blib/lib/Array/CompareAndFilter.pm

filter: Statementstmt 98.9% 189 / 191 Branchbran 97.1% 101 / 104 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 28 / 28 Podpod 78.5% 11 / 14 totaltotal 96.1% 347 / 361 CC 74 SCAR 19.7
CC
74
Cov
97%
singularize
14.0
substractValue
12.0
compareOrder
10.2
CRAP 7.2
↑ Summary
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File: CompareAndFilter.pm
5 # Date: 2012-06-25
6 # Author: H. Klausing (h.klausing (at) gmx.de)
7 # Version: v1.100
8 #
9 # Description:
10 # Compares and filters contents of arrays.
11 #
12 #
13 # Options:
14 #
15 # Tidy: -l=128 -pt=2 -sbt=2 -bt=2 -bbt=2 -csc -csci=28 -bbc -bbb -lbl=1 -sob -bar -nsfs -nolq
16 #
17 ################################################################################
18 #
19 # Updates:
20 # 2014-09-14 v1.100 H. Klausing
21 # - version handling improved, version format changed.
22 # - reason for perl 5.18 message 'Smartmatch is experimental at ...' removed.
23 # 2012-09-01 v1.0.3 H. Klausing
24 # subroutine intersection corrected - multiple lists elements were handled
25 # correctly now.
26 # version number incremented
27 # 2012-08-12 v1.0.2 H. Klausing
28 # version number incremented
29 # 2012-08-05 v1.0.1 H. Klausing
30 # Test scripts modifed, external modules eleminated.
31 # compareOrder() modified, it's using ~~ smart-match operator.
32 # Documentation updated.
33 # 2012-06-25 v1.0.0 H. Klausing
34 # Initial script version
35 #
36 ################################################################################
37 #
38 13 ▶use v5.010; # loads all features available in perl 5.10 (no real requirement)
Subroutine BEGIN: called CC=1 SCAR=0.0
39 13 our $VERSION = 'v1.100'; # Version number
40  
41 #--- ToDo list -----------------------------------------------------------------
42 #
43 #-------------------------------------------------------------------------------
44 #
45 #
46 #
47 #--- module name ------------------------
48 package Array::CompareAndFilter;
49 13 require Exporter;
50 #
51 #
52 #
53 #--- process requirements ---------------
54 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 #
57 #
58 #
59 #--- global variables -------------------
60 13 ▶use constant UNDEF => '?undef?';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 #
62 #
63 #
64 #--- Interface to caller ----------------
65 13 our @ISA = qw(Exporter);
66 13 our @EXPORT = qw(); # standard export
67 13 our @EXPORT_OK = qw(
68 compareValue compareItem compareOrder
69 intersection difference substractItem substractValue
70 unscramble
71 unique singularize singular); # export if required
72 13 our %EXPORT_TAGS = ( # Export as group
73 all => [
74 qw(compareValue compareItem compareOrder intersection difference substractItem substractValue unscramble unique singularize singular)
75 ],
76 compare => [qw(compareValue compareItem compareOrder)],
77 substract => [qw(substractItem substractValue)],
78 );
79 #
80 #
81 #
82 #--- used modules -----------------------
83 #
84 #
85 #
86 #-------------------------------------------------------------------------------
87 # compareValue matches the contents of two arrays. The result is true
88 # if all values from ARRAY1 are found in ARRAY2. The amount of found
89 # items is ignored for this test. Value is defined as a data value of
90 # an item.
91 # If an item value is undef it will be handled like the text '?undef?'
92 # compareValue([1,2,3], [2,1,3]) == 1
93 # compareValue([1,1,2], [1,2,2,1]) == 1
94 # compareValue([1,1,2], [1,1,2,2,3]) == 0
95 # Param1: reference to first array
96 # Param2: reference to second array
97 # Return: 1 = both arrays have the same value content
98 # 0 = content of arrays are not equal; this will be set if one
99 # of the following conditions were found:
100 # - different items in both arrays
101 #-------------------------------------------------------------------------------
102 25 ▶sub compareValue {
Subroutine compareValue: called CC=8 SCAR=20.8
Pod documented
103 25 my ($arr1_ref, $arr2_ref) = @_;
104 25 ▶ die "Parmeter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 23 25
105 23 ▶ die "Parmeter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 21 23
106 21 my %list1 = setItems($arr1_ref, 1);
107 21 my %list2 = setItems($arr2_ref, -1);
108  
109 # check if all items counted twice
110 21 for (keys %list1) {
111 45 ▶ return 0 if (not defined $list2{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list2{$_} 6 39 45
112 }
113  
114 15 for (keys %list2) {
115 35 ▶ return 0 if (not defined $list1{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list1{$_} 2 33 35
116 33 ▶ return 0 if ($list1{$_} + $list2{$_});
Branch: 1 branch
BranchTrueFalseTotal
if $list1{$_} + $list2{$_} 0 33 33
117 }
118 13 return 1;
119 }
120 #
121 #
122 #
123 #-------------------------------------------------------------------------------
124 # compareItem matches the contents of two arrays. The result is true
125 # if all items from ARRAY1 are found in ARRAY2. The amount of found
126 # items is important for this test.
127 # If an item value is undef it will be handled like the text '?undef?'
128 # compareItem([1,2,3,5], [2,1,4,3]) == 0
129 # compareItem([1,2,3,4], [2,1,4,3]) == 1
130 # Param1: reference to first array
131 # Param2: reference to second array
132 # Return: 1 = both arrays have the same content
133 # 0 = content of arrays are not equal; this will be set if one of
134 # the following conditions were found:
135 # - different items in both arrays
136 #-------------------------------------------------------------------------------
137 25 ▶sub compareItem {
Subroutine compareItem: called CC=5 SCAR=16.1
Pod documented
138 25 my ($arr1_ref, $arr2_ref) = @_;
139 25 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 23 25
140 23 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 21 23
141  
142 # use item value as hash key to shrink data and count the occurrence
143 21 my %count = incrementItems([(@$arr1_ref, @$arr2_ref)]); # count amount of items
144  
145 # check if all items counted twice
146 21 for (keys %count) {
147 112234 ▶ return 0 if ($count{$_} % 2);
Branch: 1 branch
BranchTrueFalseTotal
if $count{$_} % 2 8 112226 112234
148 }
149 13 return 1;
150 }
151 #
152 #
153 #
154 #-------------------------------------------------------------------------------
155 # compareOrder matches the order of two arrays. The result is true if
156 # the item values of both arrays are equal and the order are same.
157 # compareOrder([1,2,3,4], [2,1,4,3]) == 0
158 # compareOrder([1,2,3,4], [1,2,3,4]) == 1
159 # If an item value is undef it will be handled like the text '?undef?'
160 # Param1: reference to first array
161 # Param2: reference to second array
162 # Return: 1 = both arrays have the same content
163 # 0 = content of arrays are not equal; this will be set if one of
164 # the following conditions were found:
165 # - different items
166 # - sorted content is different
167 #-------------------------------------------------------------------------------
168 23 ▶sub compareOrder {
Subroutine compareOrder: called CC=10 SCAR=23.3
Pod documented
169 23 my ($arr1_ref, $arr2_ref) = @_;
170 23 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 21 23
171 21 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 3 18 21
172 18 my $size1 = scalar(@{$arr1_ref});
173 18 my $size2 = scalar(@{$arr2_ref});
174  
175 18 ▶ if ($size1 != $size2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size1 != $size2) 3 15 18
176 3 return 0;
177 }
178  
179 15 ▶ if (!defined($arr1_ref->[0]) && !defined($arr2_ref->[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $arr1_ref->[0] || defined $arr2_ref->[0]) { } 6 9 15
elsif (not defined $arr1_ref->[0] && defined $arr2_ref->[0]) { } 0 9 9
Condition66%2/3
defined $arr1_ref->[0] || defined $arr2_ref->[0]
l!l&&r!l&&!r
906
MC/DC50%1/2
defined $arr1_ref->[0] || defined $arr2_ref->[0]
defined $arr1_ref->[0] defined $arr2_ref->[0]
Truth table66%2/3
A || B
A= defined $arr1_ref->[0]B= defined $arr2_ref->[0]
ABresult
1X1
011
000
180 6 return 1;
181 ▶ } elsif (!defined($arr1_ref->[0]) || !defined($arr2_ref->[0])) {
Condition33%1/3
defined $arr1_ref->[0] && defined $arr2_ref->[0]
!ll&&!rl&&r
009
MC/DC0%0/2
defined $arr1_ref->[0] && defined $arr2_ref->[0]
defined $arr1_ref->[0] defined $arr2_ref->[0]
Truth table33%1/3
A && B
A= defined $arr1_ref->[0]B= defined $arr2_ref->[0]
ABresult
0X0
100
111
182 0 return 0;
183 }
184  
185 9 ▶ for (my $i = 0; $i < scalar(@{$arr1_ref}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$arr1_ref;}) 29 5 34
186 29 ▶ if ($arr1_ref->[$i] ne $arr2_ref->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr1_ref->[$i] ne $arr2_ref->[$i]) 4 25 29
187 4 return 0;
188 }
189 }
190 5 return 1;
191 }
192 #
193 #
194 #
195 #-------------------------------------------------------------------------------
196 # intersection get all items that a listed in both arrays.
197 # If an item value is undef it will be handled like the text '?undef?'.
198 # Before the result is returned this value is changed back to undef.
199 # intersection([1,2,5,4], [2,1,4,3]) ==> (1,2,4)
200 # intersection([1,2,4,3,4], [2,1,3]) ==> (1,2,3)
201 # Param1: reference to first array
202 # Param2: reference to second array
203 # Return: Sorted list of equal items
204 #-------------------------------------------------------------------------------
205 25 ▶sub intersection {
Subroutine intersection: called CC=7 SCAR=19.5
Pod documented
206 25 my ($arr1_ref, $arr2_ref) = @_;
207 25 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 23 25
208 23 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 21 23
209  
210 # use item value as hash key to shrink data and count the occurrence
211 21 my %cnt1 = incrementItems($arr1_ref);
212 21 my %cnt2 = incrementItems($arr2_ref);
213 21 my @resultList;
214  
215 21 foreach my $key (keys %cnt1) {
216 64 ▶ while ($cnt1{$key}--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt1{$key}--) 75 64 139
217  
218 75 ▶ if (defined($cnt2{$key}) && $cnt2{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cnt2{$key} and $cnt2{$key}) 60 15 75
Condition100%3/3
defined $cnt2{$key} and $cnt2{$key}
!ll&&!rl&&r
13260
MC/DC100%2/2
defined $cnt2{$key} and $cnt2{$key}
defined $cnt2{$key} $cnt2{$key}
Truth table100%3/3
A and B
A= defined $cnt2{$key}B= $cnt2{$key}
ABresult
0X0
100
111
219 60 push(@resultList, $key);
220 60 $cnt2{$key}--;
221 }
222 }
223 }
224 21 return prepareReturnList(\@resultList);
225 }
226 #
227 #
228 #
229 #-------------------------------------------------------------------------------
230 # difference matches the contents of two arrays.
231 # If an item value is undef it will be handled like the text '?undef?'.
232 # Before the result is returned this value is changed back to undef.
233 # difference([1,2,3,4], [1,3,4,5]) == [2,5]
234 # Param1: reference to first array
235 # Param2: reference to second array
236 # Return: Sorted list of items that are not stored in the othe array.
237 #-------------------------------------------------------------------------------
238 17 ▶sub difference {
Subroutine difference: called CC=5 SCAR=16.1
Pod documented
239 17 my ($arr1_ref, $arr2_ref) = @_;
240 17 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 15 17
241 15 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 13 15
242  
243 # use item value as hash key to shrink data and count the occurrence
244 13 my %count = incrementItems([(@$arr1_ref, @$arr2_ref)]); # count amount of items
245 13 my @diff;
246  
247 13 for (keys %count) {
248  
249 47 ▶ if ($count{$_} <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count{$_} <= 1) 16 31 47
250 16 push(@diff, $_);
251 }
252 }
253 13 return prepareReturnList(\@diff);
254 }
255 #
256 #
257 #
258 #-------------------------------------------------------------------------------
259 # substractItem removes items listed in ARRAYREF1 from the ARRAYREF1.
260 # One item in ARRAYREF1 removes one items in ARRAYREF2.
261 # If an item value is undef it will be handled like the text '?undef?'.
262 # Before the result is returned this value is changed back to undef.
263 # substractItem([1,3,4,5],[1,2,3,4]) == [5]
264 # substractItem([1,1,4,5],[1,5]) == [1,4]
265 # Param1: reference to first array (minuend)
266 # Param2: reference to second array (subtrahend)
267 # Return: difference of ARRAYREF1 - ARRAYREF2
268 #-------------------------------------------------------------------------------
269 16 ▶sub substractItem {
Subroutine substractItem: called CC=6 SCAR=17.9
Pod documented
270 16 my ($arr1_ref, $arr2_ref) = @_;
271 16 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 14 16
272 14 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 12 14
273  
274 # copy ARRAYREF1 content to return array
275 12 my @resultList = @$arr1_ref;
276  
277 12 ▶ for (my $i = 0; $i < scalar(@$arr2_ref); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$arr2_ref) 28 12 40
278 28 ▶ for (my $j = 0; $j < scalar(@resultList); $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @resultList) 41 1 42
279 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 41 ▶ if ($arr2_ref->[$i] eq $resultList[$j]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr2_ref->[$i] eq $resultList[$j]) 27 14 41
282 27 splice(@resultList, $j, 1);
283 27 last;
284 }
285 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 }
287 }
288 12 return @resultList;
289 }
290 #
291 #
292 #
293 #-------------------------------------------------------------------------------
294 # substractValue removes items listed in ARRAYREF1 from the ARRAYREF1.
295 # A value listed in ARRAYREF1 will be remove all equal values from
296 # ARRAYREF2. The function returns the undeleted items from ARRAYREF1
297 # If an item value is undef it will be handled like the text '?undef?'.
298 # Before the result is returned this value is changed back to undef.
299 # substractValue([1,2,3,4], [1,3,4,5]) == [5]
300 # substractValue([1,5], [1,1,4,5]) == [4]
301 # Param1: reference to first array
302 # Param2: reference to second array
303 # Return: list of result items
304 #-------------------------------------------------------------------------------
305 20 ▶sub substractValue {
Subroutine substractValue: called CC=12 SCAR=24.9
Pod documented
306 20 my ($arr1_ref, $arr2_ref) = @_;
307 20 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 18 20
308 18 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 16 18
309  
310 # copy ARRAYREF1 content to return array
311 16 my @arr1;
312 16 ▶ push(@arr1, (defined($_) ? $_ : UNDEF)) for (@$arr1_ref);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 60 5 65
313 16 my @arr2;
314 16 ▶ push(@arr2, (defined($_) ? $_ : UNDEF)) for (@$arr2_ref);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 31 2 33
315 16 my @resultList = (); # result list
316 16 my %subtrahend;
317 16 $subtrahend{$_} = 1 for (@arr2); # remove double item values
318 16 my @exclude = keys %subtrahend;
319  
320 0 foreach my $item (@arr1) {
321 13 ▶ no warnings; # to avoid Argument "?undef?" isn't numeric in smart match
Subroutine BEGIN: called CC=1 SCAR=0.0
322  
323 65 ▶ if (not($item ~~ @exclude)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item ~~ \@exclude) 27 38 65
324 27 ▶ if (not $item ~~ @resultList) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item ~~ \@resultList) 27 0 27
325 27 ▶ push(@resultList, ($item ne UNDEF) ? $item : undef);
Branch: 1 branch
BranchTrueFalseTotal
$item ne '?undef?' ? : 25 2 27
326 }
327 }
328 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
329 }
330 16 return @resultList;
331 }
332 #
333 #
334 #
335 #-------------------------------------------------------------------------------
336 # unscramble matches the contents of two arrays.
337 # If an item value is undef it will be handled like the text '?undef?'.
338 # Before the result is returned this value is changed back to undef.
339 # unscramble([1,2], [1,4,3]) == [1,2,3,4]
340 # Param1: reference to first array
341 # Param2: reference to second array
342 # Return: Sorted list of singular items stored in array1 and array2
343 #-------------------------------------------------------------------------------
344 18 ▶sub unscramble {
Subroutine unscramble: called CC=4 SCAR=13.9
Pod documented
345 18 my ($arr1_ref, $arr2_ref) = @_;
346 18 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 16 18
347 16 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 14 16
348  
349 # use item value as hash key to shrink data and count the occurrence
350 14 my %count = incrementItems([(@$arr1_ref, @$arr2_ref)]); # count amount of items
351 14 my @union;
352  
353 14 for (keys %count) {
354 62 push(@union, $_);
355 }
356 14 return prepareReturnList(\@union);
357 }
358 #
359 #
360 #
361 #-------------------------------------------------------------------------------
362 # unique Gets items from array1 that are not listed in array2.
363 # If an item value is undef it will be handled like the text '?undef?'.
364 # Before the result is returned this value is changed back to undef.
365 # unique([1,2,3], [2,1,4,3]) == []
366 # unique([1,2,3,4], [1,2,3,5]) == [4]
367 # unique([1,2,3,5], [1,2,3,4]) == [5]
368 # Param1: reference to first array
369 # Param2: reference to second array
370 # Return: Sorted list with items that contain in array1 but not in
371 # array2.
372 #-------------------------------------------------------------------------------
373 22 ▶sub unique {
Subroutine unique: called CC=5 SCAR=16.1
Pod documented
374 22 my ($arr1_ref, $arr2_ref) = @_;
375 22 ▶ die "Parameter 1 must be an array reference!" if (ref($arr1_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr1_ref ne "ARRAY" 2 20 22
376 20 ▶ die "Parameter 2 must be an array reference!" if (ref($arr2_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr2_ref ne "ARRAY" 2 18 20
377  
378 # use item value as hash key to shrink data and count the occurrence
379 18 my %countInc = incrementItems($arr1_ref); # count amount of item values
380 18 my %countDec = incrementItems($arr2_ref); # count amount of item values
381  
382 # check for unique items
383 18 my @unique;
384  
385 18 for (keys %countInc) {
386  
387 71 ▶ if (not defined($countDec{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $countDec{$_}) 21 50 71
388 21 push(@unique, $_);
389 }
390 }
391 18 return prepareReturnList(\@unique);
392 }
393 #
394 #
395 #
396 #-------------------------------------------------------------------------------
397 # singularize singulars all items of an array.
398 # If an item value is undef it will be handled like the text '?undef?'.
399 # Before the result is returned this value is changed back to undef.
400 # singularize([1,2,3,4,5]) == [1,2,3,4,5]
401 # singularize([1,1,1,1,2]) == [1,2]
402 # singularize([2,2,3,1,2],'s') == [1,2,3]
403 # singularize([2,2,3,1,2],'b') == [2,3,1]
404 # singularize([2,2,3,1,2],'e') == [3,1,2]
405 # Param1: reference to array
406 # Param2: order selection (default is 's')
407 # 'b' - keep order from the begin of array
408 # 'e' - keep order from the end of array
409 # all other - sort output
410 # Return: Sorted list of singular items.
411 #-------------------------------------------------------------------------------
412 40 ▶sub singularize {
Subroutine singularize: called CC=14 SCAR=26.4
Pod documented
413 40 my ($arr_ref, $order) = @_;
414 40 ▶ $order = $order // 's';
Condition100%2/2
$order // "s"
l!l
2416
MC/DC100%1/1
$order // "s"
$order
Truth table100%2/2
A
A= $order
Aresult
11
00
415 40 ▶ die "Parameter 1 must be an array reference!" if (ref($arr_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr_ref ne "ARRAY" 2 38 40
416 38 ▶ die "Parameter 2 must be a scalar!" if (ref($order) ne '');
Branch: 1 branch
BranchTrueFalseTotal
if ref $order ne "" 1 37 38
417  
418 37 ▶ if ($order =~ /^(b|e)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order =~ /^(b|e)$/i) 18 19 37
419 18 my @outList;
420  
421 18 ▶ if ($order =~ /^(b)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order =~ /^(b)$/i) { } 9 9 18
422  
423 # order from begin of array
424 9 foreach my $item (@$arr_ref) {
425 33 ▶ $item = $item // UNDEF;
Condition100%2/2
$item // "?undef?"
l!l
285
MC/DC100%1/1
$item // "?undef?"
$item
Truth table100%2/2
A
A= $item
Aresult
11
00
426  
427 33 ▶ if (not(grep {$item eq $_} @outList)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$item eq $_;} @outList) 19 14 33
428 19 push(@outList, $item);
429 }
430 }
431 } else {
432  
433 # order from end of array
434 9 ▶ for (my $i = scalar(@$arr_ref) - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 33 9 42
435 33 ▶ my $item = $arr_ref->[$i] // UNDEF;
Condition100%2/2
$arr_ref->[$i] // "?undef?"
l!l
285
MC/DC100%1/1
$arr_ref->[$i] // "?undef?"
$arr_ref->[$i]
Truth table100%2/2
A
A= $arr_ref->[$i]
Aresult
11
00
436 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
437  
438 33 ▶ if (not($item ~~ @outList)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item ~~ \@outList) 19 14 33
439 19 unshift(@outList, $item);
440 }
441 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
442 }
443 }
444  
445 18 ▶ for (my $i = 0; $i < scalar(@outList); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @outList) 36 12 48
446 36 ▶ if ($outList[$i] eq UNDEF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outList[$i] eq "?undef?") 6 30 36
447 6 $outList[$i] = undef;
448 6 last;
449 }
450 }
451 18 return @outList;
452 } ## end if ($order =~ /^(b|e)$/i)
453 19 my %count = incrementItems($arr_ref); # count amount of items
454 19 return prepareReturnList([keys %count]);
455 } ## end sub singularize
456 #
457 #
458 #
459 #-------------------------------------------------------------------------------
460 # singular is the short form of singularize([],'b').
461 # If an item value is undef this subfunction can not be used.
462 # Before the result is returned this value is changed back to undef.
463 # singular([1,2,3,4,5]) == [1,2,3,4,5]
464 # singular([1,1,1,1,2]) == [1,2]
465 # singular([2,2,3,1,2]) == [2,1,2]
466 # Param1: reference to array
467 # Return: Sorted list of singular items.
468 #-------------------------------------------------------------------------------
469 9 ▶sub singular {
Subroutine singular: called CC=2 SCAR=6.9
Pod documented
470 9 my ($arr_ref) = @_;
471 9 ▶ die "Parameter 1 must be an array reference!" if (ref($arr_ref) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr_ref ne "ARRAY" 2 7 9
472 7 my %found=();
473 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
474 7 my @outlist = grep { !$found{$_}++ } @{$arr_ref};
475 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
476  
477 7 return @outlist;
478 }
479 #
480 #
481 #
482 #-------------------------------------------------------------------------------
483 # incrementItems counts all items values. If an item value is undefined
484 # the used return key is '?undef?'.
485 # Param1: reference to array
486 # Return: hash list of counted items.
487 #-------------------------------------------------------------------------------
488 145 ▶sub incrementItems {
Subroutine incrementItems: called CC=3 SCAR=11.0
Pod undocumented
489 145 my ($arr_ref,) = @_;
490 145 my %countList;
491 145 ▶ $countList{defined() ? $_ : UNDEF}++ for (@$arr_ref);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 500688 42 500730
492 145 return %countList;
493 }
494 #
495 #
496 #
497 #-------------------------------------------------------------------------------
498 # setItems defines to each item, used as a key, a value. If an item
499 # value is undefined the used key name is '?undef?'.
500 # Param1: reference to array
501 # Param2: value that will be used to fill the value part of each
502 # key/value pair.
503 # Return: hash list of counted items.
504 #-------------------------------------------------------------------------------
505 42 ▶sub setItems {
Subroutine setItems: called CC=3 SCAR=11.0
Pod undocumented
506 42 my ($arr_ref, $value) = @_;
507 42 my %resultList;
508 42 ▶ $resultList{defined() ? $_ : UNDEF} = $value for (@$arr_ref);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 148 4 152
509 42 return %resultList;
510 }
511 #
512 #
513 #
514 #-------------------------------------------------------------------------------
515 # prepareReturnList removes the content of $unDef to undef and sorts the
516 # return values.
517 # Param1: reference to array
518 # Return: array list of items
519 #-------------------------------------------------------------------------------
520 85 ▶sub prepareReturnList {
Subroutine prepareReturnList: called CC=3 SCAR=11.0
Pod undocumented
521 85 my ($arr_ref) = @_;
522 85 my @returnList;
523 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
524 85 ▶ push(@returnList, (($_ ne UNDEF) ? $_ : undef)) for (sort @$arr_ref);
Branch: 1 branch
BranchTrueFalseTotal
$_ ne '?undef?' ? : 199 14 213
525 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
526 85 return @returnList;
527 }
528 #
529 #
530 #
531 13 1;
532  
533 =pod
534  
535 =head1 NAME
536  
537 Array::CompareAndFilter - Basic functions to compare and filter arrays
538 for different requirements.
539  
540 =head1 SYNOPSIS
541  
542 use Array::CompareAndFilter qw(compareValue compareItem compareOrder intersection difference unscramble unique);
543 # or use Array::CompareAndFilter qw(:all);
544  
545 # compare the content of two arrays
546 if(compareValue([1,2,3,3], [1,2,3])) {
547 say "Both arrays have same content."; # output
548 } else {
549 say "Arrays has different content.";
550 }
551  
552 # compare the content of two arrays
553 if(compareItem([1,2,3], [2,3,1])) {
554 say "Both arrays have same content."; # output
555 } else {
556 say "Arrays are different.";
557 }
558  
559 # compare the content and the order of two arrays
560 if(compareOrder([1,2,3], [1,2,3])) {
561 say "Both arrays have same content in the same order."; # output
562 } else {
563 say "Arrays are different.";
564 }
565  
566 # intersection gets equal items of two arrays
567 my @inter = intersection([1,2,3], [2,3,4,2]);
568 say "The intersection items (\@inter) are 2 & 3.";
569  
570 # substractItem substract ARR2 items from ARR1
571 my @subItem = substractItem([3,1,2,3], [2,3]);
572 say "The substractItem items (\@subItem) are 1 & 3";
573  
574 # substractValue substract ARR2 value from ARR1
575 my @subValue = substractValue([3,1,2,3], [2,3]);
576 say "The substractValue items (\@subValue) is 1";
577  
578 # difference gets items that are not part of the other aray
579 my @diff = difference([1,2,3,4], [1,3,4,5]);
580 say "The difference items (\@diff) are 2 & 5.";
581  
582 # union gets a list of items that part of all arrays
583 my @unscramble = unscramble([1,2], [1,4,3]);
584 say "The unscramble items (\@unscramble) are 1,2,3 & 4.";
585  
586 # unique gets a list of items of array1 that part are not in array2
587 my @unique = unique([1,2,3,4,6], [1,2,3,5]);
588 say "The unique items (@unique) are 4 & 6.";
589  
590 # singularize gets a list of singular items of array
591 my @singularize = singularize([3,2,3,4,1]);
592 say "The singularize items (\@singularize) are 1, 2, 3 & 4.";
593 my @singularize = singularize([3,2,3,4,1],'b');
594 say "The singularize items (\@singularize) are 3, 2, 4 & 1.";
595 my @singularize = singularize([3,2,3,4,1],'e');
596 say "The singularize items (\@singularize) are 2, 3, 4 & 1.";
597  
598 # singular gets a list of singular items of array from first to last
599 my @singular = singular([3,2,3,4,1]);
600 say "The singular items (\@singular) are 3, 2, 4 & 1.";
601  
602 =head1 DESCRIPTION
603  
604 This module helps to solve easy tasks with arrays. Comparing of arrays or
605 filtering array data are this kind of task that this module supports.
606  
607 =head2 Functions
608  
609 The following parameter names ARRAY, ARRAY1 and ARRAY2 are synonyms for
610 array any kind of arrays. If these names are listed in square brackets it
611 is a synonym of a reference. E.g. [ARRAY1] is a reference to the array
612 ARRAY1.
613  
614 Other parameter types will not excepted. If a given scalar value to the
615 functions has not the reference type ARRAY the function exits with an
616 error message. E.g. a call of compareValue([1,2,3], 3) will throw an error.
617  
618 =head3 compareValue
619  
620 =over 4
621  
622 =item B<compareValue>([ARRAY1],[ARRAY2])
623  
624 I<compareValue> compares all values of two arrays. If each value of one
625 array is found in the other array it will return true (1). The function
626 returns false (0) if a difference in the content was found. The
627 comparison is case sensitive. Value is defined as a data value of
628 # an item.
629  
630 If an item value is undefined it will be handled within the function like
631 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
632 value than the function returns a undef for this.
633  
634 =over 4
635  
636 =item Examples for return value 1
637  
638 compareValue([1,2,3], [3,2,1]); # returns 1
639 compareValue([1,2,3], [3,2,1,2,3]); # returns 1
640  
641 =item Examples for return value 0
642  
643 compareValue([1,2,undef], [3,2,1,2,3]); # returns 0
644 compareValue([1,2,4], [3,2,1,2,3]); # returns 0
645  
646 =back
647  
648 =back
649  
650 =head3 compareItem
651  
652 =over 4
653  
654 =item B<compareItem>([ARRAY1],[ARRAY2])
655  
656 I<compareItem> compares all items of two arrays. If the size and the
657 contents are equal this function will return 1. The function returns 0
658 if a difference in the size or in the content is found. The comparison
659 is case sensitive.
660  
661 If an item value is undefined it will be handled within the function like
662 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
663 value than the function returns a undef for this.
664  
665 =over 4
666  
667 =item Examples for return value 1
668  
669 compareItem([1,2,3], [3,2,1]); # returns 1
670 compareItem([1,2,undef], [undef,2,1]); # returns 1
671  
672 =item Examples for return value 0
673  
674 compareItem([1,2,3], [3,2,1,2,3]); # returns 0
675 compareItem([1,2,3], [4,2,1]); # returns 0
676  
677 =back
678  
679 =back
680  
681 =head3 compareOrder
682  
683 =over 4
684  
685 =item B<compareOrder>([ARRAY1],[ARRAY2])
686  
687 I<compareOrder> compares all items of two arrays. If the size, content
688 and the order of items are same it will return 1. The function returns 0
689 if a difference in size, content or order of items is found. The
690 comparison is case sensitive.
691  
692 If an item value is undefined it will be handled within the function like
693 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
694 value than the function returns a undef for this.
695  
696 =over 4
697  
698 =item Examples for return value 1
699  
700 compareOrder([1,2,3], [1,2,3]); # returns 1
701 compareOrder([undef], [undef]); # returns 1
702 compareOrder([], []); # returns 1
703  
704 =item Examples for return value 0
705  
706 compareOrder([1,2,3], [1,2,3,3]); # returns 0
707 compareOrder([1,2,3], [1,3,2]); # returns 0
708  
709 =back
710  
711 =back
712  
713 =head3 intersection
714  
715 =over 4
716  
717 =item B<intersection>([ARRAY1],[ARRAY2])
718  
719 I<intersection> returns all items that are listed in each of both arrays
720 as a sorted list. If one array has no items or no item is listed in the
721 other array this function returns an empty array. The comparison is
722 case sensitive.
723  
724 If an item value is undefined it will be handled within the function like
725 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
726 value than the function returns a undef for this.
727  
728 =over 4
729  
730 =item Examples
731  
732 intersection([1,2,3], [1,2,3]); # returns (1,2,3)
733 intersection([undef], [undef]); # returns (undef)
734 intersection([], []); # returns ()
735 intersection([1,2], [2,3]); # returns (2)
736 intersection([2,1,2], [3,1,2,2]); # returns (1,2,2)
737  
738 =back
739  
740 =back
741  
742 =head3 substractItem
743  
744 =over 4
745  
746 =item B<substractItem>([ARRAY1],[ARRAY2])
747  
748 I<substractItem> returns an array with a subtraction list of the
749 operation ARRAY1 - ARRAY2. If an item value in ARRAY2 is listed in
750 ARRAY1, than one item of ARRAY1 will be removed from the begin of the
751 list. To remove multiple items, same amount of items have to be listed
752 in ARRAY1. If no match between an item of ARRAY2 to ARRAY1 is found,
753 no change will happen in ARRAY1.
754  
755 The item order of ARRAY1 will be kept in the result list.
756  
757 =over 4
758  
759 =item Examples
760  
761 substractItem([1,2,3,4], [1,2,3]); # returns (4)
762 substractItem([undef], [undef]); # returns ()
763 substractItem([1,2], [3]); # returns (1,2)
764 substractItem([1,3,2,2], [2,1,2]); # returns (3)
765  
766 =back
767  
768 =back
769  
770 =head3 substractValue
771  
772 =over 4
773  
774 =item B<substractValue>([ARRAY1],[ARRAY2])
775  
776 I<substractValue> returns an array with a subtraction list of the
777 operation ARRAY1 - ARRAY2. A value in ARRAY2 removes all items of
778 ARRAY1 that have the same value. If no match between an item of ARRAY2
779 to ARRAY1 is found, no change will happen in ARRAY1.
780  
781 The item order of ARRAY1 will be kept in the result list.
782  
783 =over 4
784  
785 =item Examples
786  
787 substractValue([1,2,3,2,1], [1,2]); # returns (3)
788 substractValue([undef,undef], [undef]); # returns ()
789 substractValue([], [1,2]); # returns ()
790 substractValue([1,2], [1,3]); # returns (2)
791  
792 =back
793  
794 =back
795  
796 =head3 difference
797  
798 =over 4
799  
800 =item B<difference>([ARRAY1],[ARRAY2])
801  
802 I<difference> returns a list of items that are not listed in the other
803 array. The comparison is case sensitive.
804  
805 If an item value is undefined it will be handled within the function like
806 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
807 value than the function returns a undef for this.
808  
809 =over 4
810  
811 =item Examples
812  
813 difference([1,2,3,4], [1,3,4,5]); # returns (2,5)
814 difference([1], [2]); # returns (1,2)
815 difference([undef,1], [2,3,1]); # returns (2,3,undef)
816 difference([2,1], [3,1,2]); # returns (3)
817  
818 =back
819  
820 =back
821  
822  
823 =head3 unscramble
824  
825 =over 4
826  
827 =item B<unscramble>([ARRAY1],[ARRAY2])
828  
829 I<unscramble> returns a summary list of items. Each item value of
830 both arrays will exist maximal one time.
831  
832 If an item value is undefined it will be handled within the function like
833 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
834 value than the function returns a undef for this.
835  
836 =over 4
837  
838 =item Examples
839  
840 unscramble([1,2], [1,4,3]); # returns (1,2,3,4)
841 unscramble([1,1], [2,3,3,1]); # returns (1,2,3)
842 unscramble([1,1], []); # returns (1)
843  
844 =back
845  
846 =back
847  
848 =head3 unique
849  
850 =over 4
851  
852 =item B<unique>([ARRAY1],[ARRAY2])
853  
854 I<unique> checks all item values of ARRAY1 in the array of ARRAY2.
855 It will return all items which were not found in ARRAY2.
856  
857 If an item value is undefined it will be handled within the function like
858 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
859 value than the function returns a undef for this.
860  
861 =over 4
862  
863 =item Examples
864  
865 unique([1,2,3], [2,1,4,3]); # returns ()
866 unique([1,2,3,4], [1,2,3,5]); # returns (4)
867 unique([1,2,3,5], [2,3,4]); # returns (1,5)
868  
869 =back
870  
871 =back
872  
873 =head3 singularize
874  
875 =over 4
876  
877 =item B<singularize>([ARRAY])
878  
879 I<singularize> removes all double items values of the given array list.
880 By using an order argument the output can be selected for some different
881 result variations.
882  
883 'B<b>' or 'B<B>' scans the input array by the item values from the
884 begin and returns a list were the first found position of each value is
885 used. E.g. [1,2,1,3] -> (1,2,3)
886  
887 'B<e>' or 'B<E>' scans the input array by the item values from the
888 end and returns a list were the last found position of each value is
889 used. E.g. [1,2,1,3] -> (2,1,3)
890  
891 No order argument or other data then 'b|B' or 'e|B' will return a sorted
892 list of singularize values.
893  
894 If sorting is selected than following issue needes to be considered.
895 If an item value is undefined it will be handled within the function like
896 the text like '?undef?'. If an item value of ARRAY1 or ARRAY2 has the same
897 value than the function returns a undef for this.
898  
899 =over 4
900  
901 =item Examples
902  
903 singularize([qw(d b d b c a)]); # returns ('a','b','c','d')
904 singularize([3,2,3,4,1]); # returns (1,2,3,4)
905 singularize([3,2,3,4,1],'s'); # returns (1,2,3,4)
906 singularize([3,2,3,4,1],'b'); # returns (3,2,4,1)
907 singularize([3,2,3,4,1],'e'); # returns (2,3,4,1)
908  
909 =back
910  
911 =back
912  
913 =head3 singular
914  
915 =over 4
916  
917 =item B<singular>([ARRAY])
918  
919 I<singular> removes all double items values of the given array list.
920 This subfunction scans the input array by the item values from the
921 begin and returns a list were the first found position of each value is
922 used. E.g. [1,2,1,3] -> (1,2,3)
923  
924 It is expected that the array has no undefined element. Otherwise use
925 singularize().
926  
927 =over 4
928  
929 =item Examples
930  
931 singular([qw(d b d b c a)]); # returns ('d','b','c','a')
932 singular([3,2,3,4,1]); # returns (3,2,4,1)
933  
934 =back
935  
936 =back
937  
938 =head1 VERSION
939  
940 v1.100
941  
942 =head1 AUTHOR
943  
944 H. Klausing <h.klausing (at) gmx.de>
945  
946 =head1 LICENSE
947  
948 Copyright (c) 2012 H. Klausing, All Rights Reserved.
949  
950 This program is free software; you can redistribute it and/or
951 modify it under the same terms as Perl itself.
952  
953 =cut
954 __END__
↑ Summary