line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ArrayHashSearch;
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
ArrayHashSearch - Search utility for arrays and hashes in Perl.
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
596
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
5
|
use strict;
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
11
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION @EXPORT @EXPORT_OK @ISA);
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
1665
|
|
12
|
|
|
|
|
|
|
require Exporter;
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
15
|
|
|
|
|
|
|
$VERSION = '0.04';
|
16
|
|
|
|
|
|
|
@EXPORT = qw(array_contain array_deeply_contain hash_contain hash_deeply_contain deeply_contain);
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 Features
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head2 array_contain
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
this routine searches the given array for the given scalar value and stops at the first value match found
|
23
|
|
|
|
|
|
|
parameters:
|
24
|
|
|
|
|
|
|
returns 1 if an element with the given scalar value was found else 0
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub array_contain
|
29
|
|
|
|
|
|
|
{
|
30
|
1
|
|
|
1
|
1
|
9
|
my $sourcearrayref = shift @_;
|
31
|
1
|
|
|
|
|
3
|
my $targetscalar = shift @_;
|
32
|
1
|
50
|
33
|
|
|
17
|
die "\nBad arguments for subroutine 'array_contains': firstly an array reference and secondly a scalar are required!\n"
|
|
|
|
33
|
|
|
|
|
33
|
|
|
|
|
|
|
unless ((ref $sourcearrayref eq "ARRAY") and (defined ($targetscalar) and not ref($targetscalar)));
|
34
|
1
|
|
|
|
|
2
|
my $found = 0;
|
35
|
|
|
|
|
|
|
#search for the given scalar in first dimension only
|
36
|
1
|
|
|
|
|
12
|
foreach (sort @$sourcearrayref) #sort routine moves scalar elements to the beginning of array
|
37
|
|
|
|
|
|
|
{
|
38
|
8
|
100
|
|
|
|
14
|
if ($_ eq $targetscalar)
|
39
|
|
|
|
|
|
|
{
|
40
|
1
|
|
|
|
|
3
|
$found = 1;
|
41
|
1
|
|
|
|
|
2
|
last;
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
}
|
44
|
1
|
|
|
|
|
2
|
return $found;
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 array_deeply_contain
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
this routine searches the given array and any hierarchy of referenced arrays for the given scalar value and stops at the first value match found
|
50
|
|
|
|
|
|
|
parameters:
|
51
|
|
|
|
|
|
|
returns 1 if an element with the given scalar value was found else 0
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub array_deeply_contain
|
56
|
|
|
|
|
|
|
{
|
57
|
4
|
|
|
4
|
1
|
39
|
my $sourcearrayref = shift @_;
|
58
|
4
|
|
|
|
|
7
|
my $targetscalar = shift @_;
|
59
|
4
|
50
|
33
|
|
|
30
|
die "\nBad arguments for subroutine 'array_contains_recursive': firstly an array reference and secondly a scalar are required!\n"
|
|
|
|
33
|
|
|
|
|
60
|
|
|
|
|
|
|
unless ((ref $sourcearrayref eq "ARRAY") and (defined ($targetscalar) and not ref($targetscalar)));
|
61
|
4
|
|
|
|
|
5
|
my $found = 0;
|
62
|
|
|
|
|
|
|
#search for the given scalar in first dimension
|
63
|
4
|
|
|
|
|
20
|
foreach (sort @$sourcearrayref) #sort routine moves scalar elements to the beginning of array
|
64
|
|
|
|
|
|
|
{
|
65
|
17
|
100
|
100
|
|
|
74
|
if (not ref($_) and ($_ eq $targetscalar))
|
66
|
|
|
|
|
|
|
{
|
67
|
2
|
|
|
|
|
7
|
return 1;
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
}
|
70
|
|
|
|
|
|
|
#if not found yet, search for other dimensions (pointed to by references)
|
71
|
2
|
|
|
|
|
9
|
foreach (reverse sort @$sourcearrayref) #sort and reverse move references to the beginning of array
|
72
|
|
|
|
|
|
|
{
|
73
|
5
|
100
|
|
|
|
12
|
if (ref $_)
|
74
|
|
|
|
|
|
|
{
|
75
|
2
|
50
|
|
|
|
6
|
die "\nArrays with references to non arrays are not supported only 'pure' multi-dimensional arrays!\n"
|
76
|
|
|
|
|
|
|
unless (ref $_ eq "ARRAY");
|
77
|
2
|
|
|
|
|
8
|
$found = array_deeply_contain($_,$targetscalar);
|
78
|
2
|
50
|
|
|
|
10
|
return 1 if ($found);
|
79
|
|
|
|
|
|
|
}
|
80
|
|
|
|
|
|
|
}
|
81
|
0
|
|
|
|
|
0
|
return $found;
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 hash_contain
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
this routine searches the given hash for the given scalar value and stops at the first value match found
|
87
|
|
|
|
|
|
|
parameters:
|
88
|
|
|
|
|
|
|
returns 1 if an element with the given scalar value was found else 0
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub hash_contain
|
93
|
|
|
|
|
|
|
{
|
94
|
0
|
|
|
0
|
1
|
0
|
my $sourcehashref = shift @_;
|
95
|
0
|
|
|
|
|
0
|
my $targetscalar = shift @_;
|
96
|
0
|
0
|
0
|
|
|
0
|
die "\nBad arguments for subroutine 'hash_contains': firstly a hash reference and secondly a scalar are required!\n"
|
|
|
|
0
|
|
|
|
|
97
|
|
|
|
|
|
|
unless ((ref $sourcehashref eq "HASH") and (defined ($targetscalar) and not ref($targetscalar)));
|
98
|
0
|
|
|
|
|
0
|
my $found = 0;
|
99
|
|
|
|
|
|
|
#search for the given scalar in first dimension only
|
100
|
0
|
|
|
|
|
0
|
foreach (sort values %$sourcehashref) #sort routine moves scalar elements to the beginning of hash
|
101
|
|
|
|
|
|
|
{
|
102
|
0
|
0
|
|
|
|
0
|
if ($_ eq $targetscalar)
|
103
|
|
|
|
|
|
|
{
|
104
|
0
|
|
|
|
|
0
|
$found = 1;
|
105
|
0
|
|
|
|
|
0
|
last;
|
106
|
|
|
|
|
|
|
}
|
107
|
|
|
|
|
|
|
}
|
108
|
0
|
|
|
|
|
0
|
return $found;
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 hash_deeply_contain
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
this routine searches the given hash and any hierarchy of referenced hashes for the given scalar value and stops at the first value match found
|
114
|
|
|
|
|
|
|
parameters:
|
115
|
|
|
|
|
|
|
returns 1 if an element with the given scalar value was found else 0
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub hash_deeply_contain
|
120
|
|
|
|
|
|
|
{
|
121
|
3
|
|
|
3
|
1
|
29
|
my $sourcehashref = shift @_;
|
122
|
3
|
|
|
|
|
5
|
my $targetscalar = shift @_;
|
123
|
3
|
50
|
33
|
|
|
28
|
die "\nBad arguments for subroutine 'hash_contains_recursive': firstly a hash reference and secondly a scalar are required!\n"
|
|
|
|
33
|
|
|
|
|
124
|
|
|
|
|
|
|
unless ((ref $sourcehashref eq "HASH") and (defined ($targetscalar) and not ref($targetscalar)));
|
125
|
3
|
|
|
|
|
4
|
my $found = 0;
|
126
|
|
|
|
|
|
|
#search for the given scalar in first dimension
|
127
|
3
|
|
|
|
|
14
|
foreach (sort values %$sourcehashref) #sort routine moves scalar elements to the beginning of hash
|
128
|
|
|
|
|
|
|
{
|
129
|
7
|
100
|
100
|
|
|
43
|
if (not ref($_) and ($_ eq $targetscalar))
|
130
|
|
|
|
|
|
|
{
|
131
|
1
|
|
|
|
|
3
|
return 1;
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
}
|
134
|
|
|
|
|
|
|
#if not found yet, search for other dimensions (pointed to by references)
|
135
|
2
|
|
|
|
|
9
|
foreach (reverse sort values %$sourcehashref) #sort and reverse move references to the beginning of hash
|
136
|
|
|
|
|
|
|
{
|
137
|
6
|
100
|
|
|
|
13
|
if (ref $_)
|
138
|
|
|
|
|
|
|
{
|
139
|
2
|
50
|
|
|
|
5
|
die "\nHashes with references to non hashes are not supported only 'pure' multi-dimensional hashes!\n"
|
140
|
|
|
|
|
|
|
unless (ref $_ eq "HASH");
|
141
|
2
|
|
|
|
|
15
|
$found = hash_deeply_contain($_,$targetscalar);
|
142
|
2
|
50
|
|
|
|
6
|
return 1 if ($found);
|
143
|
|
|
|
|
|
|
}
|
144
|
|
|
|
|
|
|
}
|
145
|
0
|
|
|
|
|
0
|
return $found;
|
146
|
|
|
|
|
|
|
}
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 deeply_contain
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
this routine searches the given hash/array and any hierarchy of referenced hashes/arrays for the given scalar value and stops at the first value match found
|
151
|
|
|
|
|
|
|
this routine should be used for mixed data structures of arrays and hashes.
|
152
|
|
|
|
|
|
|
parameters:
|
153
|
|
|
|
|
|
|
returns 1 if an element with the given scalar value was found else 0
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub deeply_contain
|
158
|
|
|
|
|
|
|
{
|
159
|
11
|
|
|
11
|
1
|
22
|
my $sourcestructureref = shift @_;
|
160
|
11
|
|
|
|
|
12
|
my $targetscalar = shift @_;
|
161
|
11
|
50
|
66
|
|
|
82
|
die "\nBad arguments for subroutine 'structure_contains_recursive': firstly a hash or array reference and secondly a scalar are required!\n"
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
162
|
|
|
|
|
|
|
unless (((ref $sourcestructureref eq "HASH") or (ref $sourcestructureref eq "ARRAY")) and (defined ($targetscalar) and not ref($targetscalar)));
|
163
|
11
|
|
|
|
|
11
|
my $found = 0;
|
164
|
11
|
100
|
|
|
|
33
|
if (ref $sourcestructureref eq "HASH") #case of a hash
|
|
|
50
|
|
|
|
|
|
165
|
|
|
|
|
|
|
{
|
166
|
|
|
|
|
|
|
#search for the given scalar in first dimension
|
167
|
7
|
|
|
|
|
25
|
foreach (sort values %$sourcestructureref) #sort routine moves scalar elements to the beginning of hash
|
168
|
|
|
|
|
|
|
{
|
169
|
19
|
100
|
100
|
|
|
72
|
if (not ref($_) and ($_ eq $targetscalar))
|
170
|
|
|
|
|
|
|
{
|
171
|
1
|
|
|
|
|
3
|
return 1;
|
172
|
|
|
|
|
|
|
}
|
173
|
|
|
|
|
|
|
}
|
174
|
|
|
|
|
|
|
#if not found yet, search for other dimensions (pointed to by references)
|
175
|
6
|
|
|
|
|
19
|
foreach (reverse sort values %$sourcestructureref) #sort and reverse move references to the beginning of hash
|
176
|
|
|
|
|
|
|
{
|
177
|
18
|
100
|
|
|
|
36
|
if (ref $_)
|
178
|
|
|
|
|
|
|
{
|
179
|
5
|
50
|
66
|
|
|
18
|
die "\nReferences to something else than hashes or arrays are invalid!\n"
|
180
|
|
|
|
|
|
|
unless ((ref $_ eq "HASH") or (ref $_ eq "ARRAY"));
|
181
|
5
|
|
|
|
|
28
|
$found = deeply_contain($_,$targetscalar);
|
182
|
5
|
100
|
|
|
|
12
|
return 1 if ($found);
|
183
|
|
|
|
|
|
|
}
|
184
|
|
|
|
|
|
|
}
|
185
|
|
|
|
|
|
|
}
|
186
|
|
|
|
|
|
|
elsif (ref $sourcestructureref eq "ARRAY") #case of an array
|
187
|
|
|
|
|
|
|
{
|
188
|
|
|
|
|
|
|
#search for the given scalar in first dimension
|
189
|
4
|
|
|
|
|
19
|
foreach (sort @$sourcestructureref) #sort routine moves scalar elements to the beginning of array
|
190
|
|
|
|
|
|
|
{
|
191
|
17
|
100
|
100
|
|
|
60
|
if (not ref($_) and ($_ eq $targetscalar))
|
192
|
|
|
|
|
|
|
{
|
193
|
1
|
|
|
|
|
7
|
return 1;
|
194
|
|
|
|
|
|
|
}
|
195
|
|
|
|
|
|
|
}
|
196
|
|
|
|
|
|
|
#if not found yet, search for other dimensions (pointed to by references)
|
197
|
3
|
|
|
|
|
14
|
foreach (reverse sort @$sourcestructureref) #sort and reverse move references to the beginning of array
|
198
|
|
|
|
|
|
|
{
|
199
|
4
|
50
|
|
|
|
19
|
if (ref $_)
|
200
|
|
|
|
|
|
|
{
|
201
|
4
|
50
|
66
|
|
|
19
|
die "\nReferences to something else than hashes or arrays are invalid!\n"
|
202
|
|
|
|
|
|
|
unless ((ref $_ eq "HASH") or (ref $_ eq "ARRAY"));
|
203
|
4
|
|
|
|
|
11
|
$found = deeply_contain($_,$targetscalar);
|
204
|
4
|
100
|
|
|
|
12
|
return 1 if ($found);
|
205
|
|
|
|
|
|
|
}
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
}
|
208
|
|
|
|
|
|
|
}
|
209
|
3
|
|
|
|
|
31
|
return $found;
|
210
|
|
|
|
|
|
|
}
|
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
1;
|
213
|
|
|
|
|
|
|
__END__
|