blib/lib/Hash/CoerceToArray.pm

filter: Statementstmt 23.5% 16 / 68 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 2 totaltotal 17.8% 20 / 112 CC 23 SCAR 43.5
CC
23
Cov
16%
_goDeepAndCoerce
132.0
coerceArray
56.0
getMinMaxDepth
56.0
CRAP 77.2
↑ Summary
1 package Hash::CoerceToArray;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our @ISA = qw/Exporter/;
8 1 our $VERSION = '0.02';
9  
10 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our @EXPORT_OK = qw/coerceArray getMinMaxDepth/;
13  
14 1 our ($hashRefLocal,$depth);
15 0 ▶sub coerceArray {
Subroutine coerceArray: not called CC=7 SCAR=40.3
Pod undocumented
16 0 my ($hashRef, $givenDepth, $sort) = @_;
17  
18 ## This would be changed in called functions
19 ## hence 'local' declaration
20 0 local $hashRefLocal = $hashRef; ## This would be
21  
22 ## die if not a HASH REFERENCE
23 0 ▶ if (ref($hashRefLocal) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Hash::CoerceToArray::hashRefLocal ne "HASH") 0 0 0
24 0 croak 'Please provide a HashRef';
25 }
26  
27 0 ▶ if($sort && $sort !~ /^(keys|values)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort and not $sort =~ /^(keys|values)$/) 0 0 0
Condition0%0/3
$sort and not $sort =~ /^(keys|values)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$sort and not $sort =~ /^(keys|values)$/
$sort not $sort =~ /^(keys|values)$/
Truth table0%0/3
A and not(B)
A= $sortB= not $sort =~ /^(keys|values)$/
ABresult
0X0
100
111
28 0 croak 'Please provide sort option as keys|values';
29 }
30  
31 ## Use the maximum depth if not given one
32 ## This depth should be accessible to all local functions
33 ## hence 'local' declaration
34 0 ▶ local $depth = getMinMaxDepth($hashRefLocal) if (!$givenDepth);
Branch: 1 branch
BranchTrueFalseTotal
unless $givenDepth 0 0 0
35 0 ▶ $depth = $givenDepth if($givenDepth);
Branch: 1 branch
BranchTrueFalseTotal
if $givenDepth 0 0 0
36  
37 ## Recursive iteration to go where HASH REFERENCE
38 ## to ARRAY REFERENCE coercion is sought
39 0 my $counter = 1;
40 0 foreach my $rec (keys %$hashRefLocal) {
41 0 _goDeepAndCoerce($$hashRefLocal{$rec},$counter,$rec,$sort);
42 }
43  
44 0 return $hashRefLocal;
45  
46 }
47  
48 0 ▶sub _goDeepAndCoerce {
Subroutine _goDeepAndCoerce: not called CC=11 SCAR=48.8
49 0 my ($hashRef,$counter,$key,$sort) = @_;
50  
51 0 ▶ if ($depth == ($counter+1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Hash::CoerceToArray::depth == $counter + 1) 0 0 0
52  
53 ## Keys would be used as breadcrumb
54 ## to change the key value at any level
55 0 my $keyString = '$hashRefLocal->';
56  
57 0 foreach my $rec (split /\:/, $key) {
58 0 $keyString .= "{'$rec'}";
59 }
60  
61 ## Put the key, values as elements to an ARRAY
62 0 my $arrayRef;
63 0 ▶ if(!ref $hashRef) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $hashRef) { } 0 0 0
64 0 $arrayRef = $hashRef;
65 }
66 else {
67 0 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) { } 0 0 0
68 0 ▶ if ($sort eq 'keys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort eq 'keys') { } 0 0 0
69 0 foreach my $keyLocal (sort {$a cmp $b} keys %$hashRef) {
70 0 push @$arrayRef, $keyLocal, $$hashRef{$keyLocal};
71 }
72 }
73 else {
74 0 foreach my $keyLocal (sort {$$hashRef{$a} cmp $$hashRef{b}} keys %$hashRef) {
75 0 push @$arrayRef, $keyLocal, $$hashRef{$keyLocal};
76 }
77 }
78 }
79 else {
80 0 ▶ while (my ($keyLocal, $valueLocal) = each %$hashRef) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hashRef) 0 0 0
81 0 push @$arrayRef, $keyLocal, $valueLocal;
82 }
83 }
84 }
85  
86 ## Do in-place replacement and return
87 0 eval "$keyString = \$arrayRef";
88 0 return;
89 }
90  
91 0 ▶ return if (ref ($hashRef) ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $hashRef ne "HASH" 0 0 0
92  
93 0 $counter++;
94 0 foreach my $rec (keys %$hashRef) {
95 0 _goDeepAndCoerce($$hashRef{$rec},$counter,"$key:$rec",$sort);
96 }
97 }
98  
99 0 ▶sub getMinMaxDepth {
Subroutine getMinMaxDepth: not called CC=7 SCAR=40.3
Pod undocumented
100 0 my ($hashRef,$minMax) = @_;
101  
102 0 ▶ $minMax = 'max' if(!$minMax);
Branch: 1 branch
BranchTrueFalseTotal
unless $minMax 0 0 0
103  
104 0 ▶ if ($minMax !~ /^(min|max)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($minMax =~ /^(min|max)$/) 0 0 0
105 0 croak 'Please provide option for depth - min|max';
106 }
107  
108 ## Used to keep track which key at certain level
109 ## has value with maximum depth
110 0 my $maxDepthThisLevel;
111  
112 0 foreach my $rec (keys %$hashRef) {
113 ## Increment and recursively call getMinMaxDepth
114 ## If value is a hash refearence
115 0 ▶ if (ref($$hashRef{$rec}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hashRef->{$rec} eq 'HASH') { } 0 0 0
116 0 $$maxDepthThisLevel{$rec} = 1+getMinMaxDepth($$hashRef{$rec});
117 }
118 else {
119 0 $$maxDepthThisLevel{$rec} = 1;
120 }
121 }
122
123 ## Return the maximum or minimum depth as obtained in certain level
124 0 my $depth;
125 0 ▶ if ($minMax eq 'max') {
Branch: 2 branches
BranchTrueFalseTotal
if ($minMax eq 'max') { } 0 0 0
elsif ($minMax eq 'min') { } 0 0 0
126 0 $depth = (sort {$b<=>$a} values %$maxDepthThisLevel)[0];
127 }
128 elsif ($minMax eq 'min') {
129 0 $depth = (sort {$a<=>$b} values %$maxDepthThisLevel)[0];
130 }
131  
132 0 return $depth;
133 }
134  
135 1 1;
136  
137 =head1 NAME
138  
139 Hash::CoerceToArray - Find the depth of any multi-hierarchical HASH REFERENCE structure
140 - Go to any level of the HASH REFERENCE randomly and convert the value
141 against a key to an ARRAY REFERENCE if it is HASH REFERENCE
142
143 =head1 SYNOPSIS
144  
145 use Hash::CoerceToArray qw /coerceArray getMinMaxDepth/;
146
147 my $maxDepth = getMinMaxDepth (\%hash);
148 my $minDepth = getMinMaxDepth (\%hash, 'min');
149  
150 my $hashRef = coerceArray(\%hash);
151 my $hashRef = coerceArray(\%hash, $maxDepth);
152  
153 my $hashRef = coerceArray(\%hash, $maxDepth, 'keys') --> sorts at $maxDepth based on keys
154 my $hashRef = coerceArray(\%hash, $maxDepth, 'values') --> sorts at $maxDepth based on values
155
156 map {$hashRef = coerceArray($hashRef,$_);} (1..$maxDepth)
157
158 =head1 ABSTRACT
159  
160 This module allows the user to get maximum or minimum depth of a HASH REFERENCE
161 variable in a multilevel structure where values are HASH REFERENCES themselves.
162  
163 Also, user is allowed to change the HASH REFERENCE value at any level randomly
164 to an ARRAY REFERENCE. By selecting the deepest level of the HASH REFERENCE
165 structure first and calling coerceArray() subroutine from thereon to depth level
166 of 1 sequentially, user can change the whole HASH REFERENCE structure
167 to an ARRAY REFERENCE hierarchy.
168  
169 =head1 DESCRIPTION
170  
171 Example HashRef.
172  
173 my $hashRef = { 'L1_1' => {'L2_1' => {'L3_1' => 'V1',
174 'L3_2' => 'V2',
175 'L3_3' => 'V3'
176 },
177 'L2_2' => {'L3_1' => {'L4_1' => 'V1',
178 'L4_2' => 'V2',
179 },
180 },
181 },
182 'L1_2' => 'V1',
183 };
184 print getMinMaxDepth($hashRef)
185 >>>> 4
186  
187 print getMinMaxDepth($hashRef, 'min')
188 >>>> 1
189  
190 $hashRef = coerceArray($hashRef);
191 print Dumper $hashRef;
192 >>>>>
193 {
194 'L1_1' => {
195 'L2_1' => {
196 'L3_2' => 'V2',
197 'L3_3' => 'V3',
198 'L3_1' => 'V1'
199 },
200 'L2_2' => {
201 'L3_1' => [
202 'L4_1',
203 'V1',
204 'L4_2',
205 'V2'
206 ]
207 }
208 }
209 };
210  
211 $hashRef = coerceArray($hashRef,2);
212 print Dumper $hashRef;
213 >>>>>
214 {
215 'L1_1' => [
216 'L2_1',
217 {
218 'L3_2' => 'V2',
219 'L3_3' => 'V3',
220 'L3_1' => 'V1'
221 },
222 'L2_2',
223 {
224 'L3_1' => [
225 'L4_1',
226 'V1',
227 'L4_2',
228 'V2'
229 ]
230 }
231 ]
232 };
233  
234 =head1 CAVEATS
235  
236 The coerceArray() routine as of now works only if the Hash References are found continuously,
237 if any other reference like Array References occur in between, it won't work as desired.
238  
239 Eg. take the following Hash Reference which has Array Reference at Level 1
240  
241 {
242 'L1_1' => [
243 'L2_1',
244 {
245 'L3_2' => 'V2',
246 'L3_3' => 'V3',
247 'L3_1' => 'V1'
248 },
249 'L2_2',
250 {
251 'L3_1' => [
252 'L4_1',
253 'V1',
254 'L4_2',
255 'V2'
256 ]
257 }
258 ]
259 };
260 Now here $hashRef = coerceArray($hashRef,2);
261 print Dumper $hashRef; - won't work as desired.
262 I will look to improve it in a future release.
263  
264 =head1 SUPPORT
265  
266 debashish@cpan.org
267  
268 =head1 ACKNOWLEDGEMENTS
269  
270 =head1 COPYRIGHT & LICENSE
271  
272 Copyright 2013 Debashish Parasar, all rights reserved.
273  
274 This program is free software; you can redistribute it and/or modify it
275 under the same terms as Perl itself.
276  
277 =cut
↑ Summary