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