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