| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Hash::Work; # Several static functions to manipulate hash-arrays |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# This class provides several static methods to manipulate hashes. |
|
5
|
|
|
|
|
|
|
# Some of this methods you can also find in different modules, but |
|
6
|
|
|
|
|
|
|
# I wanted to provide a simplier way to import that methods to a class. |
|
7
|
|
|
|
|
|
|
# |
|
8
|
|
|
|
|
|
|
# SYNOPSIS |
|
9
|
|
|
|
|
|
|
# ======== |
|
10
|
|
|
|
|
|
|
# |
|
11
|
|
|
|
|
|
|
# # exports all functions |
|
12
|
|
|
|
|
|
|
# use Hash::Work ':all'; |
|
13
|
|
|
|
|
|
|
# |
|
14
|
|
|
|
|
|
|
# # E.g. a specific function |
|
15
|
|
|
|
|
|
|
# use Hash::Work qw/merge_hashes/; |
|
16
|
|
|
|
|
|
|
# |
|
17
|
|
|
|
|
|
|
# LICENSE |
|
18
|
|
|
|
|
|
|
# ======= |
|
19
|
|
|
|
|
|
|
# You can redistribute it and/or modify it under the conditions of LGPL. |
|
20
|
|
|
|
|
|
|
# |
|
21
|
|
|
|
|
|
|
# AUTHOR |
|
22
|
|
|
|
|
|
|
# ====== |
|
23
|
|
|
|
|
|
|
# Andreas Hernitscheck ahernit(AT)cpan.org |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
2
|
|
|
2
|
|
20013
|
use strict; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
67
|
|
|
27
|
2
|
|
|
2
|
|
33
|
use 5.006; |
|
|
2
|
|
|
|
|
5
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
|
12
|
use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
109
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
2
|
|
|
2
|
|
416
|
use Hash::Merge qw( merge ); |
|
|
2
|
|
|
|
|
1761
|
|
|
|
2
|
|
|
|
|
86
|
|
|
32
|
2
|
|
|
2
|
|
338
|
use Clone qw/clone/; |
|
|
2
|
|
|
|
|
1813
|
|
|
|
2
|
|
|
|
|
95
|
|
|
33
|
2
|
|
|
2
|
|
7
|
use Exporter; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
635
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION='0.04'; |
|
36
|
|
|
|
|
|
|
|
|
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
|
14
|
my $a=shift; |
|
87
|
1
|
|
|
|
|
1
|
my $b=shift; |
|
88
|
1
|
|
|
|
|
1
|
my %ha; |
|
89
|
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
8
|
my $mergec = Hash::Merge->new(); |
|
91
|
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
17
|
my $c = $mergec->merge( $b, $a ); |
|
93
|
|
|
|
|
|
|
|
|
94
|
1
|
50
|
|
|
|
70
|
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
|
1
|
|
my @array; |
|
185
|
0
|
0
|
|
|
|
|
@array = @_ if ref @_ eq ''; |
|
186
|
0
|
|
|
|
|
|
my $arrayref; |
|
187
|
0
|
0
|
|
|
|
|
$arrayref = shift if ref $_[0] eq 'ARRAY'; |
|
188
|
0
|
|
|
|
|
|
my %returned_hash; |
|
189
|
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if( defined $arrayref ) { |
|
191
|
0
|
|
|
|
|
|
foreach my $line_ref(@{ $arrayref }) { |
|
|
0
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
$returned_hash{$line_ref} = 1; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
} else { |
|
195
|
0
|
|
|
|
|
|
foreach my $line (@array) { |
|
196
|
0
|
|
|
|
|
|
$returned_hash{$line} = 1; |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
} |
|
199
|
0
|
0
|
|
|
|
|
return wantarray ? %returned_hash : \%returned_hash; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
#################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 NAME |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Hash::Work - Several static functions to manipulate hash-arrays |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# exports all functions |
|
219
|
|
|
|
|
|
|
use Hash::Work ':all'; |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# E.g. a specific function |
|
222
|
|
|
|
|
|
|
use Hash::Work qw/merge_hashes/; |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
This class provides several static methods to manipulate hashes. |
|
229
|
|
|
|
|
|
|
Some of this methods you can also find in different modules, but |
|
230
|
|
|
|
|
|
|
I wanted to provide a simplier way to import that methods to a class. |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 REQUIRES |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
L |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
L |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
L<5.006> |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 METHODS |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 array_to_hash |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
my \%hashref = array_to_hash(@array | \@arrayref); |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Converts simply an array to a hash by using the array entries as |
|
252
|
|
|
|
|
|
|
key and a 1 as value. Returns context specific a reference to a hash |
|
253
|
|
|
|
|
|
|
or a hash. |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 copy_hash_values |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
copy_hash_values(\%from, \%to); |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
copy the hash values from first hash to the second. |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head2 first_arrays_to_hash |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
my \%hashref = first_arrays_to_hash(\%hashref); |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
removes arrays from a hash recursively by |
|
268
|
|
|
|
|
|
|
taking first array entry. |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
example: |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
input: |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
$VAR1 = { |
|
275
|
|
|
|
|
|
|
'anode' => [ |
|
276
|
|
|
|
|
|
|
{ |
|
277
|
|
|
|
|
|
|
'name' => 'anything', |
|
278
|
|
|
|
|
|
|
'abc' => [ |
|
279
|
|
|
|
|
|
|
{ |
|
280
|
|
|
|
|
|
|
'foo' => [ |
|
281
|
|
|
|
|
|
|
'text' |
|
282
|
|
|
|
|
|
|
] |
|
283
|
|
|
|
|
|
|
} |
|
284
|
|
|
|
|
|
|
] |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
] |
|
287
|
|
|
|
|
|
|
}; |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
output: |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
$VAR1 = { |
|
292
|
|
|
|
|
|
|
'anode' => { |
|
293
|
|
|
|
|
|
|
'name' => 'anything', |
|
294
|
|
|
|
|
|
|
'abc' => { |
|
295
|
|
|
|
|
|
|
'foo' => 'text' |
|
296
|
|
|
|
|
|
|
} |
|
297
|
|
|
|
|
|
|
} |
|
298
|
|
|
|
|
|
|
}; |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
I use it to handle some results of XML::Simple, to flaten it. |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head2 merge_hashes |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
my \%hashref = merge_hashes(\%hashref1, \%hashref2); |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
takes 2 hashes and merges them by using their keys to one hash. |
|
310
|
|
|
|
|
|
|
returns a hash or hashref, depending on the context call. |
|
311
|
|
|
|
|
|
|
It uses the Hash::Merge class. |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
The second entry weights more then the second and overwrites the first values. |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
my $h1 = { |
|
317
|
|
|
|
|
|
|
'def' => '456', |
|
318
|
|
|
|
|
|
|
'foo' => 'ooo', |
|
319
|
|
|
|
|
|
|
}; |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
my $h2 = { |
|
323
|
|
|
|
|
|
|
'def' => '777', |
|
324
|
|
|
|
|
|
|
'more' => 'mmm', |
|
325
|
|
|
|
|
|
|
}; |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
my $res = merge_hashes($h1,$h2); |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
$res has then: |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
$VAR1 = { |
|
333
|
|
|
|
|
|
|
'def' => '777', |
|
334
|
|
|
|
|
|
|
'foo' => 'ooo', |
|
335
|
|
|
|
|
|
|
'more' => 'mmm' |
|
336
|
|
|
|
|
|
|
}; |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
Returns a hash or hashref. depending onthe context |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=head1 AUTHOR |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
Andreas Hernitscheck ahernit(AT)cpan.org |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=head1 LICENSE |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
You can redistribute it and/or modify it under the conditions of LGPL. |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=cut |
|
355
|
|
|
|
|
|
|
|