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