line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hash::Work; # Several static functions to manipulate hash-arrays |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION='0.03'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# This class provides several static methods to manipulate hashes. |
8
|
|
|
|
|
|
|
# Some of this methods you can also find in different modules, but |
9
|
|
|
|
|
|
|
# I wanted to provide a simplier way to import that methods to a class. |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# SYNOPSIS |
12
|
|
|
|
|
|
|
# ======== |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# # exports all functions |
15
|
|
|
|
|
|
|
# use Hash::Work ':all'; |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# # E.g. a specific function |
18
|
|
|
|
|
|
|
# use Hash::Work qw/merge_hashes/; |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# LICENSE |
21
|
|
|
|
|
|
|
# ======= |
22
|
|
|
|
|
|
|
# You can redistribute it and/or modify it under the conditions of LGPL. |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
# AUTHOR |
25
|
|
|
|
|
|
|
# ====== |
26
|
|
|
|
|
|
|
# Andreas Hernitscheck ahernit(AT)cpan.org |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
|
44084
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
30
|
2
|
|
|
2
|
|
65
|
use 5.006; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
95
|
|
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
|
23
|
use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
193
|
|
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
2
|
|
989
|
use Hash::Merge qw( merge ); |
|
2
|
|
|
|
|
2955
|
|
|
2
|
|
|
|
|
131
|
|
35
|
2
|
|
|
2
|
|
826
|
use Clone qw/clone/; |
|
2
|
|
|
|
|
3753
|
|
|
2
|
|
|
|
|
141
|
|
36
|
2
|
|
|
2
|
|
15
|
use Exporter; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1042
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => [qw( |
43
|
|
|
|
|
|
|
merge_hashes |
44
|
|
|
|
|
|
|
copy_hash_values |
45
|
|
|
|
|
|
|
first_arrays_to_hash |
46
|
|
|
|
|
|
|
array_to_hash |
47
|
|
|
|
|
|
|
)] ); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Exporter::export_ok_tags('all'); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# takes 2 hashes and merges them by using their keys to one hash. |
55
|
|
|
|
|
|
|
# returns a hash or hashref, depending on the context call. |
56
|
|
|
|
|
|
|
# It uses the Hash::Merge class. |
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# The second entry weights more then the second and overwrites the first values. |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# |
61
|
|
|
|
|
|
|
# my $h1 = { |
62
|
|
|
|
|
|
|
# 'def' => '456', |
63
|
|
|
|
|
|
|
# 'foo' => 'ooo', |
64
|
|
|
|
|
|
|
# }; |
65
|
|
|
|
|
|
|
# |
66
|
|
|
|
|
|
|
# |
67
|
|
|
|
|
|
|
# my $h2 = { |
68
|
|
|
|
|
|
|
# 'def' => '777', |
69
|
|
|
|
|
|
|
# 'more' => 'mmm', |
70
|
|
|
|
|
|
|
# }; |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# my $res = merge_hashes($h1,$h2); |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# $res has then: |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# $VAR1 = { |
78
|
|
|
|
|
|
|
# 'def' => '777', |
79
|
|
|
|
|
|
|
# 'foo' => 'ooo', |
80
|
|
|
|
|
|
|
# 'more' => 'mmm' |
81
|
|
|
|
|
|
|
# }; |
82
|
|
|
|
|
|
|
# |
83
|
|
|
|
|
|
|
# Returns a hash or hashref. depending onthe context |
84
|
|
|
|
|
|
|
# |
85
|
|
|
|
|
|
|
sub merge_hashes{ # \%hashref (\%hashref1,\%hashref2) |
86
|
1
|
|
|
1
|
1
|
18
|
my $a=shift; |
87
|
1
|
|
|
|
|
2
|
my $b=shift; |
88
|
1
|
|
|
|
|
2
|
my %ha; |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
12
|
my $mergec = Hash::Merge->new(); |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
25
|
my $c = $mergec->merge( $b, $a ); |
93
|
|
|
|
|
|
|
|
94
|
1
|
50
|
|
|
|
92
|
return wantarray ? %$c : $c; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# copy the hash values from first hash to the second. |
100
|
|
|
|
|
|
|
sub copy_hash_values{ # void (\%from,\%to) |
101
|
0
|
|
|
0
|
1
|
|
my $from=shift; |
102
|
0
|
|
|
|
|
|
my $to=shift; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
foreach my $k (keys %$from){ |
106
|
0
|
|
|
|
|
|
$to->{$k}=$from->{$k}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# removes arrays from a hash recursively by |
116
|
|
|
|
|
|
|
# taking first array entry. |
117
|
|
|
|
|
|
|
# |
118
|
|
|
|
|
|
|
# example: |
119
|
|
|
|
|
|
|
# |
120
|
|
|
|
|
|
|
# input: |
121
|
|
|
|
|
|
|
# |
122
|
|
|
|
|
|
|
# $VAR1 = { |
123
|
|
|
|
|
|
|
# 'anode' => [ |
124
|
|
|
|
|
|
|
# { |
125
|
|
|
|
|
|
|
# 'name' => 'anything', |
126
|
|
|
|
|
|
|
# 'abc' => [ |
127
|
|
|
|
|
|
|
# { |
128
|
|
|
|
|
|
|
# 'foo' => [ |
129
|
|
|
|
|
|
|
# 'text' |
130
|
|
|
|
|
|
|
# ] |
131
|
|
|
|
|
|
|
# } |
132
|
|
|
|
|
|
|
# ] |
133
|
|
|
|
|
|
|
# } |
134
|
|
|
|
|
|
|
# ] |
135
|
|
|
|
|
|
|
# }; |
136
|
|
|
|
|
|
|
# |
137
|
|
|
|
|
|
|
# output: |
138
|
|
|
|
|
|
|
# |
139
|
|
|
|
|
|
|
# $VAR1 = { |
140
|
|
|
|
|
|
|
# 'anode' => { |
141
|
|
|
|
|
|
|
# 'name' => 'anything', |
142
|
|
|
|
|
|
|
# 'abc' => { |
143
|
|
|
|
|
|
|
# 'foo' => 'text' |
144
|
|
|
|
|
|
|
# } |
145
|
|
|
|
|
|
|
# } |
146
|
|
|
|
|
|
|
# }; |
147
|
|
|
|
|
|
|
# |
148
|
|
|
|
|
|
|
# |
149
|
|
|
|
|
|
|
# I use it to handle some results of XML::Simple, to flaten it. |
150
|
|
|
|
|
|
|
# |
151
|
|
|
|
|
|
|
sub first_arrays_to_hash{ # \%hashref (\%hashref) |
152
|
0
|
|
|
0
|
1
|
|
my $h = clone(shift); |
153
|
|
|
|
|
|
|
|
154
|
0
|
0
|
|
|
|
|
if ( ref($h) eq 'HASH' ){ |
|
|
0
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
foreach my $k ( keys %$h ){ |
157
|
0
|
|
|
|
|
|
my $node = $h->{$k}; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if ( ref($node) eq 'ARRAY' ){ |
160
|
0
|
|
|
|
|
|
$h->{$k} = first_arrays_to_hash( $node->[0] ); # rewrite node |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
}elsif ( ref($h) eq 'ARRAY' ){ |
168
|
0
|
|
|
|
|
|
$h = first_arrays_to_hash($h->[0]); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
return $h; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Converts simply an array to a hash by using the array entries as |
181
|
|
|
|
|
|
|
# key and a 1 as value. Returns context specific a reference to a hash |
182
|
|
|
|
|
|
|
# or a hash. |
183
|
|
|
|
|
|
|
sub array_to_hash { # \%hashref (@array|\@arrayref) |
184
|
0
|
0
|
|
0
|
1
|
|
my @array = @_ if ref @_ eq ''; |
185
|
0
|
0
|
|
|
|
|
my $arrayref = shift if ref $_[0] eq 'ARRAY'; |
186
|
0
|
|
|
|
|
|
my %returned_hash; |
187
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
if( defined $arrayref ) { |
189
|
0
|
|
|
|
|
|
foreach my $line_ref(@{ $arrayref }) { |
|
0
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
$returned_hash{$line_ref} = 1; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} else { |
193
|
0
|
|
|
|
|
|
foreach my $line (@array) { |
194
|
0
|
|
|
|
|
|
$returned_hash{$line} = 1; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
0
|
0
|
|
|
|
|
return wantarray ? %returned_hash : \%returned_hash; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
1; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
#################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 NAME |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Hash::Work - Several static functions to manipulate hash-arrays |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 SYNOPSIS |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# exports all functions |
217
|
|
|
|
|
|
|
use Hash::Work ':all'; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# E.g. a specific function |
220
|
|
|
|
|
|
|
use Hash::Work qw/merge_hashes/; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 DESCRIPTION |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This class provides several static methods to manipulate hashes. |
227
|
|
|
|
|
|
|
Some of this methods you can also find in different modules, but |
228
|
|
|
|
|
|
|
I wanted to provide a simplier way to import that methods to a class. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 REQUIRES |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
L |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
L |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
L<5.006> |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 METHODS |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 array_to_hash |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
my \%hashref = array_to_hash(@array | \@arrayref); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Converts simply an array to a hash by using the array entries as |
250
|
|
|
|
|
|
|
key and a 1 as value. Returns context specific a reference to a hash |
251
|
|
|
|
|
|
|
or a hash. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head2 copy_hash_values |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
copy_hash_values(\%from, \%to); |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
copy the hash values from first hash to the second. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head2 first_arrays_to_hash |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
my \%hashref = first_arrays_to_hash(\%hashref); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
removes arrays from a hash recursively by |
266
|
|
|
|
|
|
|
taking first array entry. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
example: |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
input: |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
$VAR1 = { |
273
|
|
|
|
|
|
|
'anode' => [ |
274
|
|
|
|
|
|
|
{ |
275
|
|
|
|
|
|
|
'name' => 'anything', |
276
|
|
|
|
|
|
|
'abc' => [ |
277
|
|
|
|
|
|
|
{ |
278
|
|
|
|
|
|
|
'foo' => [ |
279
|
|
|
|
|
|
|
'text' |
280
|
|
|
|
|
|
|
] |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
] |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
] |
285
|
|
|
|
|
|
|
}; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
output: |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
$VAR1 = { |
290
|
|
|
|
|
|
|
'anode' => { |
291
|
|
|
|
|
|
|
'name' => 'anything', |
292
|
|
|
|
|
|
|
'abc' => { |
293
|
|
|
|
|
|
|
'foo' => 'text' |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
}; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
I use it to handle some results of XML::Simple, to flaten it. |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=head2 merge_hashes |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
my \%hashref = merge_hashes(\%hashref1, \%hashref2); |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
takes 2 hashes and merges them by using their keys to one hash. |
308
|
|
|
|
|
|
|
returns a hash or hashref, depending on the context call. |
309
|
|
|
|
|
|
|
It uses the Hash::Merge class. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
The second entry weights more then the second and overwrites the first values. |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
my $h1 = { |
315
|
|
|
|
|
|
|
'def' => '456', |
316
|
|
|
|
|
|
|
'foo' => 'ooo', |
317
|
|
|
|
|
|
|
}; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
my $h2 = { |
321
|
|
|
|
|
|
|
'def' => '777', |
322
|
|
|
|
|
|
|
'more' => 'mmm', |
323
|
|
|
|
|
|
|
}; |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
my $res = merge_hashes($h1,$h2); |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
$res has then: |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
$VAR1 = { |
331
|
|
|
|
|
|
|
'def' => '777', |
332
|
|
|
|
|
|
|
'foo' => 'ooo', |
333
|
|
|
|
|
|
|
'more' => 'mmm' |
334
|
|
|
|
|
|
|
}; |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
Returns a hash or hashref. depending onthe context |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=head1 AUTHOR |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
Andreas Hernitscheck ahernit(AT)cpan.org |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
=head1 LICENSE |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
You can redistribute it and/or modify it under the conditions of LGPL. |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=cut |
353
|
|
|
|
|
|
|
|