Commit | Line | Data |
---|---|---|
cb1a09d0 | 1 | =head1 NAME |
d74e8afc | 2 | X<data structure> X<complex data structure> X<struct> |
4633a7c4 | 3 | |
cb1a09d0 | 4 | perldsc - Perl Data Structures Cookbook |
4633a7c4 | 5 | |
cb1a09d0 | 6 | =head1 DESCRIPTION |
4633a7c4 | 7 | |
cb1e035e BF |
8 | Perl lets us have complex data structures. You can write something like |
9 | this and all of a sudden, you'd have an array with three dimensions! | |
4633a7c4 | 10 | |
84f709e7 JH |
11 | for $x (1 .. 10) { |
12 | for $y (1 .. 10) { | |
13 | for $z (1 .. 10) { | |
14 | $AoA[$x][$y][$z] = | |
15 | $x ** $y + $z; | |
4633a7c4 LW |
16 | } |
17 | } | |
18 | } | |
19 | ||
20 | Alas, however simple this may appear, underneath it's a much more | |
21 | elaborate construct than meets the eye! | |
22 | ||
19799a22 | 23 | How do you print it out? Why can't you say just C<print @AoA>? How do |
4633a7c4 | 24 | you sort it? How can you pass it to a function or get one of these back |
d1be9408 | 25 | from a function? Is it an object? Can you save it to disk to read |
4633a7c4 | 26 | back later? How do you access whole rows or columns of that matrix? Do |
4973169d | 27 | all the values have to be numeric? |
4633a7c4 LW |
28 | |
29 | As you see, it's quite easy to become confused. While some small portion | |
30 | of the blame for this can be attributed to the reference-based | |
31 | implementation, it's really more due to a lack of existing documentation with | |
32 | examples designed for the beginner. | |
33 | ||
5f05dabc | 34 | This document is meant to be a detailed but understandable treatment of the |
35 | many different sorts of data structures you might want to develop. It | |
36 | should also serve as a cookbook of examples. That way, when you need to | |
37 | create one of these complex data structures, you can just pinch, pilfer, or | |
38 | purloin a drop-in example from here. | |
4633a7c4 LW |
39 | |
40 | Let's look at each of these possible constructs in detail. There are separate | |
28757baa | 41 | sections on each of the following: |
4633a7c4 LW |
42 | |
43 | =over 5 | |
44 | ||
45 | =item * arrays of arrays | |
46 | ||
47 | =item * hashes of arrays | |
48 | ||
49 | =item * arrays of hashes | |
50 | ||
51 | =item * hashes of hashes | |
52 | ||
53 | =item * more elaborate constructs | |
54 | ||
4633a7c4 LW |
55 | =back |
56 | ||
5a964f20 TC |
57 | But for now, let's look at general issues common to all |
58 | these types of data structures. | |
4633a7c4 LW |
59 | |
60 | =head1 REFERENCES | |
d74e8afc | 61 | X<reference> X<dereference> X<dereferencing> X<pointer> |
4633a7c4 | 62 | |
1f025261 ML |
63 | The most important thing to understand about all data structures in |
64 | Perl--including multidimensional arrays--is that even though they might | |
4633a7c4 | 65 | appear otherwise, Perl C<@ARRAY>s and C<%HASH>es are all internally |
5f05dabc | 66 | one-dimensional. They can hold only scalar values (meaning a string, |
4633a7c4 LW |
67 | number, or a reference). They cannot directly contain other arrays or |
68 | hashes, but instead contain I<references> to other arrays or hashes. | |
d74e8afc | 69 | X<multidimensional array> X<array, multidimensional> |
4633a7c4 | 70 | |
d1be9408 | 71 | You can't use a reference to an array or hash in quite the same way that you |
5f05dabc | 72 | would a real array or hash. For C or C++ programmers unused to |
73 | distinguishing between arrays and pointers to the same, this can be | |
74 | confusing. If so, just think of it as the difference between a structure | |
75 | and a pointer to a structure. | |
4633a7c4 | 76 | |
ba555bf5 TH |
77 | You can (and should) read more about references in L<perlref>. |
78 | Briefly, references are rather like pointers that know what they | |
4633a7c4 | 79 | point to. (Objects are also a kind of reference, but we won't be needing |
4973169d | 80 | them right away--if ever.) This means that when you have something which |
81 | looks to you like an access to a two-or-more-dimensional array and/or hash, | |
82 | what's really going on is that the base type is | |
4633a7c4 LW |
83 | merely a one-dimensional entity that contains references to the next |
84 | level. It's just that you can I<use> it as though it were a | |
85 | two-dimensional one. This is actually the way almost all C | |
86 | multidimensional arrays work as well. | |
87 | ||
19799a22 GS |
88 | $array[7][12] # array of arrays |
89 | $array[7]{string} # array of hashes | |
4633a7c4 LW |
90 | $hash{string}[7] # hash of arrays |
91 | $hash{string}{'another string'} # hash of hashes | |
92 | ||
5f05dabc | 93 | Now, because the top level contains only references, if you try to print |
4633a7c4 LW |
94 | out your array in with a simple print() function, you'll get something |
95 | that doesn't look very nice, like this: | |
96 | ||
84f709e7 | 97 | @AoA = ( [2, 3], [4, 5, 7], [0] ); |
19799a22 | 98 | print $AoA[1][2]; |
4633a7c4 | 99 | 7 |
19799a22 | 100 | print @AoA; |
4633a7c4 LW |
101 | ARRAY(0x83c38)ARRAY(0x8b194)ARRAY(0x8b1d0) |
102 | ||
103 | ||
104 | That's because Perl doesn't (ever) implicitly dereference your variables. | |
105 | If you want to get at the thing a reference is referring to, then you have | |
106 | to do this yourself using either prefix typing indicators, like | |
107 | C<${$blah}>, C<@{$blah}>, C<@{$blah[$i]}>, or else postfix pointer arrows, | |
108 | like C<$a-E<gt>[3]>, C<$h-E<gt>{fred}>, or even C<$ob-E<gt>method()-E<gt>[3]>. | |
109 | ||
110 | =head1 COMMON MISTAKES | |
111 | ||
112 | The two most common mistakes made in constructing something like | |
113 | an array of arrays is either accidentally counting the number of | |
114 | elements or else taking a reference to the same memory location | |
115 | repeatedly. Here's the case where you just get the count instead | |
116 | of a nested array: | |
117 | ||
84f709e7 JH |
118 | for $i (1..10) { |
119 | @array = somefunc($i); | |
120 | $AoA[$i] = @array; # WRONG! | |
4973169d | 121 | } |
4633a7c4 | 122 | |
19799a22 | 123 | That's just the simple case of assigning an array to a scalar and getting |
4633a7c4 LW |
124 | its element count. If that's what you really and truly want, then you |
125 | might do well to consider being a tad more explicit about it, like this: | |
126 | ||
84f709e7 JH |
127 | for $i (1..10) { |
128 | @array = somefunc($i); | |
129 | $counts[$i] = scalar @array; | |
4973169d | 130 | } |
4633a7c4 | 131 | |
84f709e7 JH |
132 | Here's the case of taking a reference to the same memory location |
133 | again and again: | |
4633a7c4 | 134 | |
84f709e7 JH |
135 | for $i (1..10) { |
136 | @array = somefunc($i); | |
137 | $AoA[$i] = \@array; # WRONG! | |
138 | } | |
139 | ||
140 | So, what's the big problem with that? It looks right, doesn't it? | |
141 | After all, I just told you that you need an array of references, so by | |
142 | golly, you've made me one! | |
143 | ||
144 | Unfortunately, while this is true, it's still broken. All the references | |
145 | in @AoA refer to the I<very same place>, and they will therefore all hold | |
146 | whatever was last in @array! It's similar to the problem demonstrated in | |
147 | the following C program: | |
148 | ||
149 | #include <pwd.h> | |
150 | main() { | |
151 | struct passwd *getpwnam(), *rp, *dp; | |
152 | rp = getpwnam("root"); | |
153 | dp = getpwnam("daemon"); | |
154 | ||
155 | printf("daemon name is %s\nroot name is %s\n", | |
156 | dp->pw_name, rp->pw_name); | |
157 | } | |
158 | ||
159 | Which will print | |
160 | ||
161 | daemon name is daemon | |
162 | root name is daemon | |
163 | ||
164 | The problem is that both C<rp> and C<dp> are pointers to the same location | |
165 | in memory! In C, you'd have to remember to malloc() yourself some new | |
166 | memory. In Perl, you'll want to use the array constructor C<[]> or the | |
167 | hash constructor C<{}> instead. Here's the right way to do the preceding | |
168 | broken code fragments: | |
d74e8afc | 169 | X<[]> X<{}> |
84f709e7 JH |
170 | |
171 | for $i (1..10) { | |
172 | @array = somefunc($i); | |
173 | $AoA[$i] = [ @array ]; | |
4973169d | 174 | } |
4633a7c4 LW |
175 | |
176 | The square brackets make a reference to a new array with a I<copy> | |
84f709e7 JH |
177 | of what's in @array at the time of the assignment. This is what |
178 | you want. | |
4633a7c4 LW |
179 | |
180 | Note that this will produce something similar, but it's | |
181 | much harder to read: | |
182 | ||
84f709e7 JH |
183 | for $i (1..10) { |
184 | @array = 0 .. $i; | |
185 | @{$AoA[$i]} = @array; | |
4973169d | 186 | } |
4633a7c4 LW |
187 | |
188 | Is it the same? Well, maybe so--and maybe not. The subtle difference | |
189 | is that when you assign something in square brackets, you know for sure | |
190 | it's always a brand new reference with a new I<copy> of the data. | |
b5d81ce9 | 191 | Something else could be going on in this new case with the C<@{$AoA[$i]}> |
4633a7c4 | 192 | dereference on the left-hand-side of the assignment. It all depends on |
19799a22 GS |
193 | whether C<$AoA[$i]> had been undefined to start with, or whether it |
194 | already contained a reference. If you had already populated @AoA with | |
4633a7c4 LW |
195 | references, as in |
196 | ||
19799a22 | 197 | $AoA[3] = \@another_array; |
4633a7c4 LW |
198 | |
199 | Then the assignment with the indirection on the left-hand-side would | |
200 | use the existing reference that was already there: | |
201 | ||
84f709e7 | 202 | @{$AoA[3]} = @array; |
4633a7c4 LW |
203 | |
204 | Of course, this I<would> have the "interesting" effect of clobbering | |
19799a22 | 205 | @another_array. (Have you ever noticed how when a programmer says |
4633a7c4 LW |
206 | something is "interesting", that rather than meaning "intriguing", |
207 | they're disturbingly more apt to mean that it's "annoying", | |
208 | "difficult", or both? :-) | |
209 | ||
5f05dabc | 210 | So just remember always to use the array or hash constructors with C<[]> |
4633a7c4 | 211 | or C<{}>, and you'll be fine, although it's not always optimally |
4973169d | 212 | efficient. |
4633a7c4 LW |
213 | |
214 | Surprisingly, the following dangerous-looking construct will | |
215 | actually work out fine: | |
216 | ||
84f709e7 JH |
217 | for $i (1..10) { |
218 | my @array = somefunc($i); | |
219 | $AoA[$i] = \@array; | |
4973169d | 220 | } |
4633a7c4 LW |
221 | |
222 | That's because my() is more of a run-time statement than it is a | |
223 | compile-time declaration I<per se>. This means that the my() variable is | |
224 | remade afresh each time through the loop. So even though it I<looks> as | |
225 | though you stored the same variable reference each time, you actually did | |
226 | not! This is a subtle distinction that can produce more efficient code at | |
227 | the risk of misleading all but the most experienced of programmers. So I | |
228 | usually advise against teaching it to beginners. In fact, except for | |
229 | passing arguments to functions, I seldom like to see the gimme-a-reference | |
230 | operator (backslash) used much at all in code. Instead, I advise | |
231 | beginners that they (and most of the rest of us) should try to use the | |
232 | much more easily understood constructors C<[]> and C<{}> instead of | |
233 | relying upon lexical (or dynamic) scoping and hidden reference-counting to | |
234 | do the right thing behind the scenes. | |
235 | ||
236 | In summary: | |
237 | ||
84f709e7 JH |
238 | $AoA[$i] = [ @array ]; # usually best |
239 | $AoA[$i] = \@array; # perilous; just how my() was that array? | |
240 | @{ $AoA[$i] } = @array; # way too tricky for most programmers | |
4633a7c4 LW |
241 | |
242 | ||
4973169d | 243 | =head1 CAVEAT ON PRECEDENCE |
d74e8afc | 244 | X<dereference, precedence> X<dereferencing, precedence> |
4633a7c4 | 245 | |
84f709e7 | 246 | Speaking of things like C<@{$AoA[$i]}>, the following are actually the |
4633a7c4 | 247 | same thing: |
d74e8afc | 248 | X<< -> >> |
4633a7c4 | 249 | |
19799a22 GS |
250 | $aref->[2][2] # clear |
251 | $$aref[2][2] # confusing | |
4633a7c4 LW |
252 | |
253 | That's because Perl's precedence rules on its five prefix dereferencers | |
254 | (which look like someone swearing: C<$ @ * % &>) make them bind more | |
255 | tightly than the postfix subscripting brackets or braces! This will no | |
256 | doubt come as a great shock to the C or C++ programmer, who is quite | |
257 | accustomed to using C<*a[i]> to mean what's pointed to by the I<i'th> | |
258 | element of C<a>. That is, they first take the subscript, and only then | |
259 | dereference the thing at that subscript. That's fine in C, but this isn't C. | |
260 | ||
19799a22 GS |
261 | The seemingly equivalent construct in Perl, C<$$aref[$i]> first does |
262 | the deref of $aref, making it take $aref as a reference to an | |
4633a7c4 | 263 | array, and then dereference that, and finally tell you the I<i'th> value |
19799a22 GS |
264 | of the array pointed to by $AoA. If you wanted the C notion, you'd have to |
265 | write C<${$AoA[$i]}> to force the C<$AoA[$i]> to get evaluated first | |
4633a7c4 LW |
266 | before the leading C<$> dereferencer. |
267 | ||
268 | =head1 WHY YOU SHOULD ALWAYS C<use strict> | |
269 | ||
270 | If this is starting to sound scarier than it's worth, relax. Perl has | |
271 | some features to help you avoid its most common pitfalls. The best | |
272 | way to avoid getting confused is to start every program like this: | |
273 | ||
274 | #!/usr/bin/perl -w | |
275 | use strict; | |
276 | ||
277 | This way, you'll be forced to declare all your variables with my() and | |
278 | also disallow accidental "symbolic dereferencing". Therefore if you'd done | |
279 | this: | |
280 | ||
19799a22 | 281 | my $aref = [ |
84f709e7 JH |
282 | [ "fred", "barney", "pebbles", "bambam", "dino", ], |
283 | [ "homer", "bart", "marge", "maggie", ], | |
284 | [ "george", "jane", "elroy", "judy", ], | |
4633a7c4 LW |
285 | ]; |
286 | ||
19799a22 | 287 | print $aref[2][2]; |
4633a7c4 LW |
288 | |
289 | The compiler would immediately flag that as an error I<at compile time>, | |
19799a22 | 290 | because you were accidentally accessing C<@aref>, an undeclared |
5f05dabc | 291 | variable, and it would thereby remind you to write instead: |
4633a7c4 | 292 | |
19799a22 | 293 | print $aref->[2][2] |
4633a7c4 LW |
294 | |
295 | =head1 DEBUGGING | |
d74e8afc ITB |
296 | X<data structure, debugging> X<complex data structure, debugging> |
297 | X<AoA, debugging> X<HoA, debugging> X<AoH, debugging> X<HoH, debugging> | |
298 | X<array of arrays, debugging> X<hash of arrays, debugging> | |
299 | X<array of hashes, debugging> X<hash of hashes, debugging> | |
4633a7c4 | 300 | |
cb1e035e BF |
301 | You can use the debugger's C<x> command to dump out complex data structures. |
302 | For example, given the assignment to $AoA above, here's the debugger output: | |
4633a7c4 | 303 | |
19799a22 GS |
304 | DB<1> x $AoA |
305 | $AoA = ARRAY(0x13b5a0) | |
4633a7c4 LW |
306 | 0 ARRAY(0x1f0a24) |
307 | 0 'fred' | |
308 | 1 'barney' | |
309 | 2 'pebbles' | |
310 | 3 'bambam' | |
311 | 4 'dino' | |
312 | 1 ARRAY(0x13b558) | |
313 | 0 'homer' | |
314 | 1 'bart' | |
315 | 2 'marge' | |
316 | 3 'maggie' | |
317 | 2 ARRAY(0x13b540) | |
318 | 0 'george' | |
319 | 1 'jane' | |
5f05dabc | 320 | 2 'elroy' |
4633a7c4 LW |
321 | 3 'judy' |
322 | ||
cb1a09d0 AD |
323 | =head1 CODE EXAMPLES |
324 | ||
54310121 | 325 | Presented with little comment (these will get their own manpages someday) |
4973169d | 326 | here are short code examples illustrating access of various |
cb1a09d0 AD |
327 | types of data structures. |
328 | ||
19799a22 | 329 | =head1 ARRAYS OF ARRAYS |
d74e8afc | 330 | X<array of arrays> X<AoA> |
cb1a09d0 | 331 | |
d1be9408 | 332 | =head2 Declaration of an ARRAY OF ARRAYS |
cb1a09d0 | 333 | |
84f709e7 JH |
334 | @AoA = ( |
335 | [ "fred", "barney" ], | |
336 | [ "george", "jane", "elroy" ], | |
337 | [ "homer", "marge", "bart" ], | |
cb1a09d0 AD |
338 | ); |
339 | ||
d1be9408 | 340 | =head2 Generation of an ARRAY OF ARRAYS |
cb1a09d0 AD |
341 | |
342 | # reading from file | |
343 | while ( <> ) { | |
19799a22 | 344 | push @AoA, [ split ]; |
4973169d | 345 | } |
cb1a09d0 AD |
346 | |
347 | # calling a function | |
84f709e7 | 348 | for $i ( 1 .. 10 ) { |
19799a22 | 349 | $AoA[$i] = [ somefunc($i) ]; |
4973169d | 350 | } |
cb1a09d0 AD |
351 | |
352 | # using temp vars | |
84f709e7 JH |
353 | for $i ( 1 .. 10 ) { |
354 | @tmp = somefunc($i); | |
355 | $AoA[$i] = [ @tmp ]; | |
4973169d | 356 | } |
cb1a09d0 AD |
357 | |
358 | # add to an existing row | |
84f709e7 | 359 | push @{ $AoA[0] }, "wilma", "betty"; |
cb1a09d0 | 360 | |
d1be9408 | 361 | =head2 Access and Printing of an ARRAY OF ARRAYS |
cb1a09d0 AD |
362 | |
363 | # one element | |
84f709e7 | 364 | $AoA[0][0] = "Fred"; |
cb1a09d0 AD |
365 | |
366 | # another element | |
19799a22 | 367 | $AoA[1][1] =~ s/(\w)/\u$1/; |
cb1a09d0 AD |
368 | |
369 | # print the whole thing with refs | |
84f709e7 | 370 | for $aref ( @AoA ) { |
cb1a09d0 | 371 | print "\t [ @$aref ],\n"; |
4973169d | 372 | } |
cb1a09d0 AD |
373 | |
374 | # print the whole thing with indices | |
84f709e7 JH |
375 | for $i ( 0 .. $#AoA ) { |
376 | print "\t [ @{$AoA[$i]} ],\n"; | |
4973169d | 377 | } |
cb1a09d0 AD |
378 | |
379 | # print the whole thing one at a time | |
84f709e7 JH |
380 | for $i ( 0 .. $#AoA ) { |
381 | for $j ( 0 .. $#{ $AoA[$i] } ) { | |
382 | print "elt $i $j is $AoA[$i][$j]\n"; | |
cb1a09d0 | 383 | } |
4973169d | 384 | } |
cb1a09d0 | 385 | |
19799a22 | 386 | =head1 HASHES OF ARRAYS |
d74e8afc | 387 | X<hash of arrays> X<HoA> |
cb1a09d0 | 388 | |
19799a22 | 389 | =head2 Declaration of a HASH OF ARRAYS |
cb1a09d0 | 390 | |
84f709e7 JH |
391 | %HoA = ( |
392 | flintstones => [ "fred", "barney" ], | |
393 | jetsons => [ "george", "jane", "elroy" ], | |
394 | simpsons => [ "homer", "marge", "bart" ], | |
cb1a09d0 AD |
395 | ); |
396 | ||
19799a22 | 397 | =head2 Generation of a HASH OF ARRAYS |
cb1a09d0 AD |
398 | |
399 | # reading from file | |
400 | # flintstones: fred barney wilma dino | |
401 | while ( <> ) { | |
84f709e7 | 402 | next unless s/^(.*?):\s*//; |
19799a22 | 403 | $HoA{$1} = [ split ]; |
4973169d | 404 | } |
cb1a09d0 AD |
405 | |
406 | # reading from file; more temps | |
407 | # flintstones: fred barney wilma dino | |
84f709e7 JH |
408 | while ( $line = <> ) { |
409 | ($who, $rest) = split /:\s*/, $line, 2; | |
410 | @fields = split ' ', $rest; | |
411 | $HoA{$who} = [ @fields ]; | |
4973169d | 412 | } |
cb1a09d0 AD |
413 | |
414 | # calling a function that returns a list | |
84f709e7 | 415 | for $group ( "simpsons", "jetsons", "flintstones" ) { |
19799a22 | 416 | $HoA{$group} = [ get_family($group) ]; |
4973169d | 417 | } |
cb1a09d0 AD |
418 | |
419 | # likewise, but using temps | |
84f709e7 JH |
420 | for $group ( "simpsons", "jetsons", "flintstones" ) { |
421 | @members = get_family($group); | |
422 | $HoA{$group} = [ @members ]; | |
4973169d | 423 | } |
cb1a09d0 AD |
424 | |
425 | # append new members to an existing family | |
84f709e7 | 426 | push @{ $HoA{"flintstones"} }, "wilma", "betty"; |
cb1a09d0 | 427 | |
19799a22 | 428 | =head2 Access and Printing of a HASH OF ARRAYS |
cb1a09d0 AD |
429 | |
430 | # one element | |
84f709e7 | 431 | $HoA{flintstones}[0] = "Fred"; |
cb1a09d0 AD |
432 | |
433 | # another element | |
19799a22 | 434 | $HoA{simpsons}[1] =~ s/(\w)/\u$1/; |
cb1a09d0 AD |
435 | |
436 | # print the whole thing | |
84f709e7 JH |
437 | foreach $family ( keys %HoA ) { |
438 | print "$family: @{ $HoA{$family} }\n" | |
4973169d | 439 | } |
cb1a09d0 AD |
440 | |
441 | # print the whole thing with indices | |
84f709e7 JH |
442 | foreach $family ( keys %HoA ) { |
443 | print "family: "; | |
444 | foreach $i ( 0 .. $#{ $HoA{$family} } ) { | |
19799a22 | 445 | print " $i = $HoA{$family}[$i]"; |
cb1a09d0 AD |
446 | } |
447 | print "\n"; | |
4973169d | 448 | } |
cb1a09d0 AD |
449 | |
450 | # print the whole thing sorted by number of members | |
84f709e7 | 451 | foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) { |
19799a22 | 452 | print "$family: @{ $HoA{$family} }\n" |
4973169d | 453 | } |
cb1a09d0 AD |
454 | |
455 | # print the whole thing sorted by number of members and name | |
84f709e7 JH |
456 | foreach $family ( sort { |
457 | @{$HoA{$b}} <=> @{$HoA{$a}} | |
458 | || | |
459 | $a cmp $b | |
460 | } keys %HoA ) | |
461 | { | |
19799a22 | 462 | print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n"; |
4973169d | 463 | } |
cb1a09d0 | 464 | |
19799a22 | 465 | =head1 ARRAYS OF HASHES |
d74e8afc | 466 | X<array of hashes> X<AoH> |
cb1a09d0 | 467 | |
d1be9408 | 468 | =head2 Declaration of an ARRAY OF HASHES |
cb1a09d0 | 469 | |
84f709e7 | 470 | @AoH = ( |
cb1a09d0 | 471 | { |
84f709e7 JH |
472 | Lead => "fred", |
473 | Friend => "barney", | |
cb1a09d0 AD |
474 | }, |
475 | { | |
84f709e7 JH |
476 | Lead => "george", |
477 | Wife => "jane", | |
478 | Son => "elroy", | |
cb1a09d0 AD |
479 | }, |
480 | { | |
84f709e7 JH |
481 | Lead => "homer", |
482 | Wife => "marge", | |
483 | Son => "bart", | |
cb1a09d0 AD |
484 | } |
485 | ); | |
486 | ||
d1be9408 | 487 | =head2 Generation of an ARRAY OF HASHES |
cb1a09d0 AD |
488 | |
489 | # reading from file | |
490 | # format: LEAD=fred FRIEND=barney | |
491 | while ( <> ) { | |
84f709e7 JH |
492 | $rec = {}; |
493 | for $field ( split ) { | |
494 | ($key, $value) = split /=/, $field; | |
495 | $rec->{$key} = $value; | |
cb1a09d0 | 496 | } |
19799a22 | 497 | push @AoH, $rec; |
4973169d | 498 | } |
cb1a09d0 AD |
499 | |
500 | ||
501 | # reading from file | |
502 | # format: LEAD=fred FRIEND=barney | |
503 | # no temp | |
504 | while ( <> ) { | |
19799a22 | 505 | push @AoH, { split /[\s+=]/ }; |
4973169d | 506 | } |
cb1a09d0 | 507 | |
19799a22 | 508 | # calling a function that returns a key/value pair list, like |
84f709e7 JH |
509 | # "lead","fred","daughter","pebbles" |
510 | while ( %fields = getnextpairset() ) { | |
19799a22 | 511 | push @AoH, { %fields }; |
4973169d | 512 | } |
cb1a09d0 AD |
513 | |
514 | # likewise, but using no temp vars | |
515 | while (<>) { | |
19799a22 | 516 | push @AoH, { parsepairs($_) }; |
4973169d | 517 | } |
cb1a09d0 AD |
518 | |
519 | # add key/value to an element | |
84f709e7 | 520 | $AoH[0]{pet} = "dino"; |
19799a22 | 521 | $AoH[2]{pet} = "santa's little helper"; |
cb1a09d0 | 522 | |
d1be9408 | 523 | =head2 Access and Printing of an ARRAY OF HASHES |
cb1a09d0 AD |
524 | |
525 | # one element | |
84f709e7 | 526 | $AoH[0]{lead} = "fred"; |
cb1a09d0 AD |
527 | |
528 | # another element | |
19799a22 | 529 | $AoH[1]{lead} =~ s/(\w)/\u$1/; |
cb1a09d0 AD |
530 | |
531 | # print the whole thing with refs | |
84f709e7 JH |
532 | for $href ( @AoH ) { |
533 | print "{ "; | |
534 | for $role ( keys %$href ) { | |
535 | print "$role=$href->{$role} "; | |
cb1a09d0 AD |
536 | } |
537 | print "}\n"; | |
4973169d | 538 | } |
cb1a09d0 AD |
539 | |
540 | # print the whole thing with indices | |
84f709e7 | 541 | for $i ( 0 .. $#AoH ) { |
cb1a09d0 | 542 | print "$i is { "; |
84f709e7 JH |
543 | for $role ( keys %{ $AoH[$i] } ) { |
544 | print "$role=$AoH[$i]{$role} "; | |
cb1a09d0 AD |
545 | } |
546 | print "}\n"; | |
4973169d | 547 | } |
cb1a09d0 AD |
548 | |
549 | # print the whole thing one at a time | |
84f709e7 JH |
550 | for $i ( 0 .. $#AoH ) { |
551 | for $role ( keys %{ $AoH[$i] } ) { | |
552 | print "elt $i $role is $AoH[$i]{$role}\n"; | |
cb1a09d0 | 553 | } |
4973169d | 554 | } |
cb1a09d0 AD |
555 | |
556 | =head1 HASHES OF HASHES | |
8e0aa7ce | 557 | X<hash of hashes> X<HoH> |
cb1a09d0 AD |
558 | |
559 | =head2 Declaration of a HASH OF HASHES | |
560 | ||
84f709e7 | 561 | %HoH = ( |
28757baa | 562 | flintstones => { |
84f709e7 JH |
563 | lead => "fred", |
564 | pal => "barney", | |
cb1a09d0 | 565 | }, |
28757baa | 566 | jetsons => { |
84f709e7 JH |
567 | lead => "george", |
568 | wife => "jane", | |
569 | "his boy" => "elroy", | |
4973169d | 570 | }, |
28757baa | 571 | simpsons => { |
84f709e7 JH |
572 | lead => "homer", |
573 | wife => "marge", | |
574 | kid => "bart", | |
4973169d | 575 | }, |
576 | ); | |
cb1a09d0 AD |
577 | |
578 | =head2 Generation of a HASH OF HASHES | |
579 | ||
580 | # reading from file | |
581 | # flintstones: lead=fred pal=barney wife=wilma pet=dino | |
582 | while ( <> ) { | |
84f709e7 JH |
583 | next unless s/^(.*?):\s*//; |
584 | $who = $1; | |
585 | for $field ( split ) { | |
586 | ($key, $value) = split /=/, $field; | |
cb1a09d0 AD |
587 | $HoH{$who}{$key} = $value; |
588 | } | |
589 | ||
590 | ||
591 | # reading from file; more temps | |
592 | while ( <> ) { | |
84f709e7 JH |
593 | next unless s/^(.*?):\s*//; |
594 | $who = $1; | |
595 | $rec = {}; | |
cb1a09d0 | 596 | $HoH{$who} = $rec; |
84f709e7 JH |
597 | for $field ( split ) { |
598 | ($key, $value) = split /=/, $field; | |
599 | $rec->{$key} = $value; | |
cb1a09d0 | 600 | } |
4973169d | 601 | } |
cb1a09d0 | 602 | |
cb1a09d0 | 603 | # calling a function that returns a key,value hash |
84f709e7 | 604 | for $group ( "simpsons", "jetsons", "flintstones" ) { |
cb1a09d0 | 605 | $HoH{$group} = { get_family($group) }; |
4973169d | 606 | } |
cb1a09d0 AD |
607 | |
608 | # likewise, but using temps | |
84f709e7 JH |
609 | for $group ( "simpsons", "jetsons", "flintstones" ) { |
610 | %members = get_family($group); | |
cb1a09d0 | 611 | $HoH{$group} = { %members }; |
4973169d | 612 | } |
cb1a09d0 AD |
613 | |
614 | # append new members to an existing family | |
84f709e7 JH |
615 | %new_folks = ( |
616 | wife => "wilma", | |
617 | pet => "dino", | |
cb1a09d0 | 618 | ); |
4973169d | 619 | |
84f709e7 | 620 | for $what (keys %new_folks) { |
cb1a09d0 | 621 | $HoH{flintstones}{$what} = $new_folks{$what}; |
4973169d | 622 | } |
cb1a09d0 AD |
623 | |
624 | =head2 Access and Printing of a HASH OF HASHES | |
625 | ||
626 | # one element | |
84f709e7 | 627 | $HoH{flintstones}{wife} = "wilma"; |
cb1a09d0 AD |
628 | |
629 | # another element | |
630 | $HoH{simpsons}{lead} =~ s/(\w)/\u$1/; | |
631 | ||
632 | # print the whole thing | |
84f709e7 | 633 | foreach $family ( keys %HoH ) { |
1fef88e7 | 634 | print "$family: { "; |
84f709e7 JH |
635 | for $role ( keys %{ $HoH{$family} } ) { |
636 | print "$role=$HoH{$family}{$role} "; | |
cb1a09d0 AD |
637 | } |
638 | print "}\n"; | |
4973169d | 639 | } |
cb1a09d0 AD |
640 | |
641 | # print the whole thing somewhat sorted | |
84f709e7 | 642 | foreach $family ( sort keys %HoH ) { |
1fef88e7 | 643 | print "$family: { "; |
84f709e7 JH |
644 | for $role ( sort keys %{ $HoH{$family} } ) { |
645 | print "$role=$HoH{$family}{$role} "; | |
cb1a09d0 AD |
646 | } |
647 | print "}\n"; | |
4973169d | 648 | } |
cb1a09d0 | 649 | |
84f709e7 | 650 | |
cb1a09d0 | 651 | # print the whole thing sorted by number of members |
84f709e7 | 652 | foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) { |
1fef88e7 | 653 | print "$family: { "; |
84f709e7 JH |
654 | for $role ( sort keys %{ $HoH{$family} } ) { |
655 | print "$role=$HoH{$family}{$role} "; | |
cb1a09d0 AD |
656 | } |
657 | print "}\n"; | |
4973169d | 658 | } |
cb1a09d0 AD |
659 | |
660 | # establish a sort order (rank) for each role | |
84f709e7 JH |
661 | $i = 0; |
662 | for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i } | |
cb1a09d0 AD |
663 | |
664 | # now print the whole thing sorted by number of members | |
84f709e7 | 665 | foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } keys %HoH ) { |
1fef88e7 | 666 | print "$family: { "; |
cb1a09d0 | 667 | # and print these according to rank order |
84f709e7 JH |
668 | for $role ( sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) { |
669 | print "$role=$HoH{$family}{$role} "; | |
cb1a09d0 AD |
670 | } |
671 | print "}\n"; | |
4973169d | 672 | } |
cb1a09d0 AD |
673 | |
674 | ||
675 | =head1 MORE ELABORATE RECORDS | |
d74e8afc | 676 | X<record> X<structure> X<struct> |
cb1a09d0 AD |
677 | |
678 | =head2 Declaration of MORE ELABORATE RECORDS | |
679 | ||
680 | Here's a sample showing how to create and use a record whose fields are of | |
681 | many different sorts: | |
682 | ||
84f709e7 | 683 | $rec = { |
4973169d | 684 | TEXT => $string, |
685 | SEQUENCE => [ @old_values ], | |
686 | LOOKUP => { %some_table }, | |
687 | THATCODE => \&some_function, | |
688 | THISCODE => sub { $_[0] ** $_[1] }, | |
689 | HANDLE => \*STDOUT, | |
cb1a09d0 AD |
690 | }; |
691 | ||
4973169d | 692 | print $rec->{TEXT}; |
cb1a09d0 | 693 | |
84f709e7 JH |
694 | print $rec->{SEQUENCE}[0]; |
695 | $last = pop @ { $rec->{SEQUENCE} }; | |
cb1a09d0 | 696 | |
84f709e7 JH |
697 | print $rec->{LOOKUP}{"key"}; |
698 | ($first_k, $first_v) = each %{ $rec->{LOOKUP} }; | |
cb1a09d0 | 699 | |
84f709e7 JH |
700 | $answer = $rec->{THATCODE}->($arg); |
701 | $answer = $rec->{THISCODE}->($arg1, $arg2); | |
cb1a09d0 AD |
702 | |
703 | # careful of extra block braces on fh ref | |
4973169d | 704 | print { $rec->{HANDLE} } "a string\n"; |
cb1a09d0 AD |
705 | |
706 | use FileHandle; | |
4973169d | 707 | $rec->{HANDLE}->autoflush(1); |
708 | $rec->{HANDLE}->print(" a string\n"); | |
cb1a09d0 AD |
709 | |
710 | =head2 Declaration of a HASH OF COMPLEX RECORDS | |
711 | ||
84f709e7 | 712 | %TV = ( |
28757baa | 713 | flintstones => { |
84f709e7 | 714 | series => "flintstones", |
4973169d | 715 | nights => [ qw(monday thursday friday) ], |
cb1a09d0 | 716 | members => [ |
84f709e7 JH |
717 | { name => "fred", role => "lead", age => 36, }, |
718 | { name => "wilma", role => "wife", age => 31, }, | |
719 | { name => "pebbles", role => "kid", age => 4, }, | |
cb1a09d0 AD |
720 | ], |
721 | }, | |
722 | ||
28757baa | 723 | jetsons => { |
84f709e7 | 724 | series => "jetsons", |
4973169d | 725 | nights => [ qw(wednesday saturday) ], |
cb1a09d0 | 726 | members => [ |
84f709e7 JH |
727 | { name => "george", role => "lead", age => 41, }, |
728 | { name => "jane", role => "wife", age => 39, }, | |
729 | { name => "elroy", role => "kid", age => 9, }, | |
cb1a09d0 AD |
730 | ], |
731 | }, | |
732 | ||
28757baa | 733 | simpsons => { |
84f709e7 | 734 | series => "simpsons", |
4973169d | 735 | nights => [ qw(monday) ], |
cb1a09d0 | 736 | members => [ |
84f709e7 JH |
737 | { name => "homer", role => "lead", age => 34, }, |
738 | { name => "marge", role => "wife", age => 37, }, | |
739 | { name => "bart", role => "kid", age => 11, }, | |
cb1a09d0 AD |
740 | ], |
741 | }, | |
742 | ); | |
743 | ||
744 | =head2 Generation of a HASH OF COMPLEX RECORDS | |
745 | ||
84f709e7 JH |
746 | # reading from file |
747 | # this is most easily done by having the file itself be | |
748 | # in the raw data format as shown above. perl is happy | |
749 | # to parse complex data structures if declared as data, so | |
750 | # sometimes it's easiest to do that | |
cb1a09d0 | 751 | |
84f709e7 JH |
752 | # here's a piece by piece build up |
753 | $rec = {}; | |
754 | $rec->{series} = "flintstones"; | |
cb1a09d0 AD |
755 | $rec->{nights} = [ find_days() ]; |
756 | ||
84f709e7 | 757 | @members = (); |
cb1a09d0 | 758 | # assume this file in field=value syntax |
84f709e7 JH |
759 | while (<>) { |
760 | %fields = split /[\s=]+/; | |
cb1a09d0 AD |
761 | push @members, { %fields }; |
762 | } | |
763 | $rec->{members} = [ @members ]; | |
764 | ||
765 | # now remember the whole thing | |
766 | $TV{ $rec->{series} } = $rec; | |
767 | ||
84f709e7 JH |
768 | ########################################################### |
769 | # now, you might want to make interesting extra fields that | |
770 | # include pointers back into the same data structure so if | |
771 | # change one piece, it changes everywhere, like for example | |
772 | # if you wanted a {kids} field that was a reference | |
773 | # to an array of the kids' records without having duplicate | |
774 | # records and thus update problems. | |
775 | ########################################################### | |
776 | foreach $family (keys %TV) { | |
777 | $rec = $TV{$family}; # temp pointer | |
778 | @kids = (); | |
779 | for $person ( @{ $rec->{members} } ) { | |
780 | if ($person->{role} =~ /kid|son|daughter/) { | |
cb1a09d0 AD |
781 | push @kids, $person; |
782 | } | |
783 | } | |
784 | # REMEMBER: $rec and $TV{$family} point to same data!! | |
785 | $rec->{kids} = [ @kids ]; | |
786 | } | |
787 | ||
84f709e7 JH |
788 | # you copied the array, but the array itself contains pointers |
789 | # to uncopied objects. this means that if you make bart get | |
790 | # older via | |
cb1a09d0 AD |
791 | |
792 | $TV{simpsons}{kids}[0]{age}++; | |
793 | ||
84f709e7 JH |
794 | # then this would also change in |
795 | print $TV{simpsons}{members}[2]{age}; | |
cb1a09d0 | 796 | |
84f709e7 JH |
797 | # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2] |
798 | # both point to the same underlying anonymous hash table | |
6ba6f0ec | 799 | |
84f709e7 JH |
800 | # print the whole thing |
801 | foreach $family ( keys %TV ) { | |
802 | print "the $family"; | |
803 | print " is on during @{ $TV{$family}{nights} }\n"; | |
804 | print "its members are:\n"; | |
805 | for $who ( @{ $TV{$family}{members} } ) { | |
cb1a09d0 AD |
806 | print " $who->{name} ($who->{role}), age $who->{age}\n"; |
807 | } | |
84f709e7 JH |
808 | print "it turns out that $TV{$family}{lead} has "; |
809 | print scalar ( @{ $TV{$family}{kids} } ), " kids named "; | |
810 | print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } ); | |
811 | print "\n"; | |
cb1a09d0 AD |
812 | } |
813 | ||
c07a80fd | 814 | =head1 Database Ties |
815 | ||
816 | You cannot easily tie a multilevel data structure (such as a hash of | |
817 | hashes) to a dbm file. The first problem is that all but GDBM and | |
818 | Berkeley DB have size limitations, but beyond that, you also have problems | |
819 | with how references are to be represented on disk. One experimental | |
5f05dabc | 820 | module that does partially attempt to address this need is the MLDBM |
f102b883 | 821 | module. Check your nearest CPAN site as described in L<perlmodlib> for |
c07a80fd | 822 | source code to MLDBM. |
823 | ||
4633a7c4 LW |
824 | =head1 SEE ALSO |
825 | ||
ba555bf5 | 826 | L<perlref>, L<perllol>, L<perldata>, L<perlobj> |
4633a7c4 LW |
827 | |
828 | =head1 AUTHOR | |
829 | ||
9607fc9c | 830 | Tom Christiansen <F<tchrist@perl.com>> |