Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

perldsc

Perl 5 version 10.1 documentation
Recently read

perldsc

NAME

perldsc - Perl Data Structures Cookbook

DESCRIPTION

The single feature most sorely lacking in the Perl programming language prior to its 5.0 release was complex data structures. Even without direct language support, some valiant programmers did manage to emulate them, but it was hard work and not for the faint of heart. You could occasionally get away with the $m{$AoA,$b} notation borrowed from awk in which the keys are actually more like a single concatenated string "$AoA$b" , but traversal and sorting were difficult. More desperate programmers even hacked Perl's internal symbol table directly, a strategy that proved hard to develop and maintain--to put it mildly.

The 5.0 release of Perl let us have complex data structures. You may now write something like this and all of a sudden, you'd have an array with three dimensions!

  1. for $x (1 .. 10) {
  2. for $y (1 .. 10) {
  3. for $z (1 .. 10) {
  4. $AoA[$x][$y][$z] =
  5. $x ** $y + $z;
  6. }
  7. }
  8. }

Alas, however simple this may appear, underneath it's a much more elaborate construct than meets the eye!

How do you print it out? Why can't you say just print @AoA ? How do you sort it? How can you pass it to a function or get one of these back from a function? Is it an object? Can you save it to disk to read back later? How do you access whole rows or columns of that matrix? Do all the values have to be numeric?

As you see, it's quite easy to become confused. While some small portion of the blame for this can be attributed to the reference-based implementation, it's really more due to a lack of existing documentation with examples designed for the beginner.

This document is meant to be a detailed but understandable treatment of the many different sorts of data structures you might want to develop. It should also serve as a cookbook of examples. That way, when you need to create one of these complex data structures, you can just pinch, pilfer, or purloin a drop-in example from here.

Let's look at each of these possible constructs in detail. There are separate sections on each of the following:

  • arrays of arrays
  • hashes of arrays
  • arrays of hashes
  • hashes of hashes
  • more elaborate constructs

But for now, let's look at general issues common to all these types of data structures.

REFERENCES

The most important thing to understand about all data structures in Perl--including multidimensional arrays--is that even though they might appear otherwise, Perl @ARRAY s and %HASH es are all internally one-dimensional. They can hold only scalar values (meaning a string, number, or a reference). They cannot directly contain other arrays or hashes, but instead contain references to other arrays or hashes.

You can't use a reference to an array or hash in quite the same way that you would a real array or hash. For C or C++ programmers unused to distinguishing between arrays and pointers to the same, this can be confusing. If so, just think of it as the difference between a structure and a pointer to a structure.

You can (and should) read more about references in the perlref(1) man page. Briefly, references are rather like pointers that know what they point to. (Objects are also a kind of reference, but we won't be needing them right away--if ever.) This means that when you have something which looks to you like an access to a two-or-more-dimensional array and/or hash, what's really going on is that the base type is merely a one-dimensional entity that contains references to the next level. It's just that you can use it as though it were a two-dimensional one. This is actually the way almost all C multidimensional arrays work as well.

  1. $array[7][12] # array of arrays
  2. $array[7]{string} # array of hashes
  3. $hash{string}[7] # hash of arrays
  4. $hash{string}{'another string'} # hash of hashes

Now, because the top level contains only references, if you try to print out your array in with a simple print() function, you'll get something that doesn't look very nice, like this:

  1. @AoA = ( [2, 3], [4, 5, 7], [0] );
  2. print $AoA[1][2];
  3. 7
  4. print @AoA;
  5. ARRAY(0x83c38)ARRAY(0x8b194)ARRAY(0x8b1d0)

That's because Perl doesn't (ever) implicitly dereference your variables. If you want to get at the thing a reference is referring to, then you have to do this yourself using either prefix typing indicators, like ${$blah} , @{$blah} , @{$blah[$i]} , or else postfix pointer arrows, like $a->[3] , $h->{fred} , or even $ob->method()->[3] .

COMMON MISTAKES

The two most common mistakes made in constructing something like an array of arrays is either accidentally counting the number of elements or else taking a reference to the same memory location repeatedly. Here's the case where you just get the count instead of a nested array:

  1. for $i (1..10) {
  2. @array = somefunc($i);
  3. $AoA[$i] = @array; # WRONG!
  4. }

That's just the simple case of assigning an array to a scalar and getting its element count. If that's what you really and truly want, then you might do well to consider being a tad more explicit about it, like this:

  1. for $i (1..10) {
  2. @array = somefunc($i);
  3. $counts[$i] = scalar @array;
  4. }

Here's the case of taking a reference to the same memory location again and again:

  1. for $i (1..10) {
  2. @array = somefunc($i);
  3. $AoA[$i] = \@array; # WRONG!
  4. }

So, what's the big problem with that? It looks right, doesn't it? After all, I just told you that you need an array of references, so by golly, you've made me one!

Unfortunately, while this is true, it's still broken. All the references in @AoA refer to the very same place, and they will therefore all hold whatever was last in @array! It's similar to the problem demonstrated in the following C program:

  1. #include <pwd.h>
  2. main() {
  3. struct passwd *getpwnam(), *rp, *dp;
  4. rp = getpwnam("root");
  5. dp = getpwnam("daemon");
  6. printf("daemon name is %s\nroot name is %s\n",
  7. dp->pw_name, rp->pw_name);
  8. }

Which will print

  1. daemon name is daemon
  2. root name is daemon

The problem is that both rp and dp are pointers to the same location in memory! In C, you'd have to remember to malloc() yourself some new memory. In Perl, you'll want to use the array constructor [] or the hash constructor {} instead. Here's the right way to do the preceding broken code fragments:

  1. for $i (1..10) {
  2. @array = somefunc($i);
  3. $AoA[$i] = [ @array ];
  4. }

The square brackets make a reference to a new array with a copy of what's in @array at the time of the assignment. This is what you want.

Note that this will produce something similar, but it's much harder to read:

  1. for $i (1..10) {
  2. @array = 0 .. $i;
  3. @{$AoA[$i]} = @array;
  4. }

Is it the same? Well, maybe so--and maybe not. The subtle difference is that when you assign something in square brackets, you know for sure it's always a brand new reference with a new copy of the data. Something else could be going on in this new case with the @{$AoA[$i]} dereference on the left-hand-side of the assignment. It all depends on whether $AoA[$i] had been undefined to start with, or whether it already contained a reference. If you had already populated @AoA with references, as in

  1. $AoA[3] = \@another_array;

Then the assignment with the indirection on the left-hand-side would use the existing reference that was already there:

  1. @{$AoA[3]} = @array;

Of course, this would have the "interesting" effect of clobbering @another_array. (Have you ever noticed how when a programmer says something is "interesting", that rather than meaning "intriguing", they're disturbingly more apt to mean that it's "annoying", "difficult", or both? :-)

So just remember always to use the array or hash constructors with [] or {} , and you'll be fine, although it's not always optimally efficient.

Surprisingly, the following dangerous-looking construct will actually work out fine:

  1. for $i (1..10) {
  2. my @array = somefunc($i);
  3. $AoA[$i] = \@array;
  4. }

That's because my() is more of a run-time statement than it is a compile-time declaration per se. This means that the my() variable is remade afresh each time through the loop. So even though it looks as though you stored the same variable reference each time, you actually did not! This is a subtle distinction that can produce more efficient code at the risk of misleading all but the most experienced of programmers. So I usually advise against teaching it to beginners. In fact, except for passing arguments to functions, I seldom like to see the gimme-a-reference operator (backslash) used much at all in code. Instead, I advise beginners that they (and most of the rest of us) should try to use the much more easily understood constructors [] and {} instead of relying upon lexical (or dynamic) scoping and hidden reference-counting to do the right thing behind the scenes.

In summary:

  1. $AoA[$i] = [ @array ]; # usually best
  2. $AoA[$i] = \@array; # perilous; just how my() was that array?
  3. @{ $AoA[$i] } = @array; # way too tricky for most programmers

CAVEAT ON PRECEDENCE

Speaking of things like @{$AoA[$i]} , the following are actually the same thing:

  1. $aref->[2][2] # clear
  2. $$aref[2][2] # confusing

That's because Perl's precedence rules on its five prefix dereferencers (which look like someone swearing: $ @ * % & ) make them bind more tightly than the postfix subscripting brackets or braces! This will no doubt come as a great shock to the C or C++ programmer, who is quite accustomed to using *a[i] to mean what's pointed to by the i'th element of a . That is, they first take the subscript, and only then dereference the thing at that subscript. That's fine in C, but this isn't C.

The seemingly equivalent construct in Perl, $$aref[$i] first does the deref of $aref, making it take $aref as a reference to an array, and then dereference that, and finally tell you the i'th value of the array pointed to by $AoA. If you wanted the C notion, you'd have to write ${$AoA[$i]} to force the $AoA[$i] to get evaluated first before the leading $ dereferencer.

WHY YOU SHOULD ALWAYS use strict

If this is starting to sound scarier than it's worth, relax. Perl has some features to help you avoid its most common pitfalls. The best way to avoid getting confused is to start every program like this:

  1. #!/usr/bin/perl -w
  2. use strict;

This way, you'll be forced to declare all your variables with my() and also disallow accidental "symbolic dereferencing". Therefore if you'd done this:

  1. my $aref = [
  2. [ "fred", "barney", "pebbles", "bambam", "dino", ],
  3. [ "homer", "bart", "marge", "maggie", ],
  4. [ "george", "jane", "elroy", "judy", ],
  5. ];
  6. print $aref[2][2];

The compiler would immediately flag that as an error at compile time, because you were accidentally accessing @aref , an undeclared variable, and it would thereby remind you to write instead:

  1. print $aref->[2][2]

DEBUGGING

Before version 5.002, the standard Perl debugger didn't do a very nice job of printing out complex data structures. With 5.002 or above, the debugger includes several new features, including command line editing as well as the x command to dump out complex data structures. For example, given the assignment to $AoA above, here's the debugger output:

  1. DB<1> x $AoA
  2. $AoA = ARRAY(0x13b5a0)
  3. 0 ARRAY(0x1f0a24)
  4. 0 'fred'
  5. 1 'barney'
  6. 2 'pebbles'
  7. 3 'bambam'
  8. 4 'dino'
  9. 1 ARRAY(0x13b558)
  10. 0 'homer'
  11. 1 'bart'
  12. 2 'marge'
  13. 3 'maggie'
  14. 2 ARRAY(0x13b540)
  15. 0 'george'
  16. 1 'jane'
  17. 2 'elroy'
  18. 3 'judy'

CODE EXAMPLES

Presented with little comment (these will get their own manpages someday) here are short code examples illustrating access of various types of data structures.

ARRAYS OF ARRAYS

Declaration of an ARRAY OF ARRAYS

  1. @AoA = (
  2. [ "fred", "barney" ],
  3. [ "george", "jane", "elroy" ],
  4. [ "homer", "marge", "bart" ],
  5. );

Generation of an ARRAY OF ARRAYS

  1. # reading from file
  2. while ( <> ) {
  3. push @AoA, [ split ];
  4. }
  5. # calling a function
  6. for $i ( 1 .. 10 ) {
  7. $AoA[$i] = [ somefunc($i) ];
  8. }
  9. # using temp vars
  10. for $i ( 1 .. 10 ) {
  11. @tmp = somefunc($i);
  12. $AoA[$i] = [ @tmp ];
  13. }
  14. # add to an existing row
  15. push @{ $AoA[0] }, "wilma", "betty";

Access and Printing of an ARRAY OF ARRAYS

  1. # one element
  2. $AoA[0][0] = "Fred";
  3. # another element
  4. $AoA[1][1] =~ s/(\w)/\u$1/;
  5. # print the whole thing with refs
  6. for $aref ( @AoA ) {
  7. print "\t [ @$aref ],\n";
  8. }
  9. # print the whole thing with indices
  10. for $i ( 0 .. $#AoA ) {
  11. print "\t [ @{$AoA[$i]} ],\n";
  12. }
  13. # print the whole thing one at a time
  14. for $i ( 0 .. $#AoA ) {
  15. for $j ( 0 .. $#{ $AoA[$i] } ) {
  16. print "elt $i $j is $AoA[$i][$j]\n";
  17. }
  18. }

HASHES OF ARRAYS

Declaration of a HASH OF ARRAYS

  1. %HoA = (
  2. flintstones => [ "fred", "barney" ],
  3. jetsons => [ "george", "jane", "elroy" ],
  4. simpsons => [ "homer", "marge", "bart" ],
  5. );

Generation of a HASH OF ARRAYS

  1. # reading from file
  2. # flintstones: fred barney wilma dino
  3. while ( <> ) {
  4. next unless s/^(.*?):\s*//;
  5. $HoA{$1} = [ split ];
  6. }
  7. # reading from file; more temps
  8. # flintstones: fred barney wilma dino
  9. while ( $line = <> ) {
  10. ($who, $rest) = split /:\s*/, $line, 2;
  11. @fields = split ' ', $rest;
  12. $HoA{$who} = [ @fields ];
  13. }
  14. # calling a function that returns a list
  15. for $group ( "simpsons", "jetsons", "flintstones" ) {
  16. $HoA{$group} = [ get_family($group) ];
  17. }
  18. # likewise, but using temps
  19. for $group ( "simpsons", "jetsons", "flintstones" ) {
  20. @members = get_family($group);
  21. $HoA{$group} = [ @members ];
  22. }
  23. # append new members to an existing family
  24. push @{ $HoA{"flintstones"} }, "wilma", "betty";

Access and Printing of a HASH OF ARRAYS

  1. # one element
  2. $HoA{flintstones}[0] = "Fred";
  3. # another element
  4. $HoA{simpsons}[1] =~ s/(\w)/\u$1/;
  5. # print the whole thing
  6. foreach $family ( keys %HoA ) {
  7. print "$family: @{ $HoA{$family} }\n"
  8. }
  9. # print the whole thing with indices
  10. foreach $family ( keys %HoA ) {
  11. print "family: ";
  12. foreach $i ( 0 .. $#{ $HoA{$family} } ) {
  13. print " $i = $HoA{$family}[$i]";
  14. }
  15. print "\n";
  16. }
  17. # print the whole thing sorted by number of members
  18. foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) {
  19. print "$family: @{ $HoA{$family} }\n"
  20. }
  21. # print the whole thing sorted by number of members and name
  22. foreach $family ( sort {
  23. @{$HoA{$b}} <=> @{$HoA{$a}}
  24. ||
  25. $a cmp $b
  26. } keys %HoA )
  27. {
  28. print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";
  29. }

ARRAYS OF HASHES

Declaration of an ARRAY OF HASHES

  1. @AoH = (
  2. {
  3. Lead => "fred",
  4. Friend => "barney",
  5. },
  6. {
  7. Lead => "george",
  8. Wife => "jane",
  9. Son => "elroy",
  10. },
  11. {
  12. Lead => "homer",
  13. Wife => "marge",
  14. Son => "bart",
  15. }
  16. );

Generation of an ARRAY OF HASHES

  1. # reading from file
  2. # format: LEAD=fred FRIEND=barney
  3. while ( <> ) {
  4. $rec = {};
  5. for $field ( split ) {
  6. ($key, $value) = split /=/, $field;
  7. $rec->{$key} = $value;
  8. }
  9. push @AoH, $rec;
  10. }
  11. # reading from file
  12. # format: LEAD=fred FRIEND=barney
  13. # no temp
  14. while ( <> ) {
  15. push @AoH, { split /[\s+=]/ };
  16. }
  17. # calling a function that returns a key/value pair list, like
  18. # "lead","fred","daughter","pebbles"
  19. while ( %fields = getnextpairset() ) {
  20. push @AoH, { %fields };
  21. }
  22. # likewise, but using no temp vars
  23. while (<>) {
  24. push @AoH, { parsepairs($_) };
  25. }
  26. # add key/value to an element
  27. $AoH[0]{pet} = "dino";
  28. $AoH[2]{pet} = "santa's little helper";

Access and Printing of an ARRAY OF HASHES

  1. # one element
  2. $AoH[0]{lead} = "fred";
  3. # another element
  4. $AoH[1]{lead} =~ s/(\w)/\u$1/;
  5. # print the whole thing with refs
  6. for $href ( @AoH ) {
  7. print "{ ";
  8. for $role ( keys %$href ) {
  9. print "$role=$href->{$role} ";
  10. }
  11. print "}\n";
  12. }
  13. # print the whole thing with indices
  14. for $i ( 0 .. $#AoH ) {
  15. print "$i is { ";
  16. for $role ( keys %{ $AoH[$i] } ) {
  17. print "$role=$AoH[$i]{$role} ";
  18. }
  19. print "}\n";
  20. }
  21. # print the whole thing one at a time
  22. for $i ( 0 .. $#AoH ) {
  23. for $role ( keys %{ $AoH[$i] } ) {
  24. print "elt $i $role is $AoH[$i]{$role}\n";
  25. }
  26. }

HASHES OF HASHES

Declaration of a HASH OF HASHES

  1. %HoH = (
  2. flintstones => {
  3. lead => "fred",
  4. pal => "barney",
  5. },
  6. jetsons => {
  7. lead => "george",
  8. wife => "jane",
  9. "his boy" => "elroy",
  10. },
  11. simpsons => {
  12. lead => "homer",
  13. wife => "marge",
  14. kid => "bart",
  15. },
  16. );

Generation of a HASH OF HASHES

  1. # reading from file
  2. # flintstones: lead=fred pal=barney wife=wilma pet=dino
  3. while ( <> ) {
  4. next unless s/^(.*?):\s*//;
  5. $who = $1;
  6. for $field ( split ) {
  7. ($key, $value) = split /=/, $field;
  8. $HoH{$who}{$key} = $value;
  9. }
  10. # reading from file; more temps
  11. while ( <> ) {
  12. next unless s/^(.*?):\s*//;
  13. $who = $1;
  14. $rec = {};
  15. $HoH{$who} = $rec;
  16. for $field ( split ) {
  17. ($key, $value) = split /=/, $field;
  18. $rec->{$key} = $value;
  19. }
  20. }
  21. # calling a function that returns a key,value hash
  22. for $group ( "simpsons", "jetsons", "flintstones" ) {
  23. $HoH{$group} = { get_family($group) };
  24. }
  25. # likewise, but using temps
  26. for $group ( "simpsons", "jetsons", "flintstones" ) {
  27. %members = get_family($group);
  28. $HoH{$group} = { %members };
  29. }
  30. # append new members to an existing family
  31. %new_folks = (
  32. wife => "wilma",
  33. pet => "dino",
  34. );
  35. for $what (keys %new_folks) {
  36. $HoH{flintstones}{$what} = $new_folks{$what};
  37. }

Access and Printing of a HASH OF HASHES

  1. # one element
  2. $HoH{flintstones}{wife} = "wilma";
  3. # another element
  4. $HoH{simpsons}{lead} =~ s/(\w)/\u$1/;
  5. # print the whole thing
  6. foreach $family ( keys %HoH ) {
  7. print "$family: { ";
  8. for $role ( keys %{ $HoH{$family} } ) {
  9. print "$role=$HoH{$family}{$role} ";
  10. }
  11. print "}\n";
  12. }
  13. # print the whole thing somewhat sorted
  14. foreach $family ( sort keys %HoH ) {
  15. print "$family: { ";
  16. for $role ( sort keys %{ $HoH{$family} } ) {
  17. print "$role=$HoH{$family}{$role} ";
  18. }
  19. print "}\n";
  20. }
  21. # print the whole thing sorted by number of members
  22. foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) {
  23. print "$family: { ";
  24. for $role ( sort keys %{ $HoH{$family} } ) {
  25. print "$role=$HoH{$family}{$role} ";
  26. }
  27. print "}\n";
  28. }
  29. # establish a sort order (rank) for each role
  30. $i = 0;
  31. for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }
  32. # now print the whole thing sorted by number of members
  33. foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } keys %HoH ) {
  34. print "$family: { ";
  35. # and print these according to rank order
  36. for $role ( sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) {
  37. print "$role=$HoH{$family}{$role} ";
  38. }
  39. print "}\n";
  40. }

MORE ELABORATE RECORDS

Declaration of MORE ELABORATE RECORDS

Here's a sample showing how to create and use a record whose fields are of many different sorts:

  1. $rec = {
  2. TEXT => $string,
  3. SEQUENCE => [ @old_values ],
  4. LOOKUP => { %some_table },
  5. THATCODE => \&some_function,
  6. THISCODE => sub { $_[0] ** $_[1] },
  7. HANDLE => \*STDOUT,
  8. };
  9. print $rec->{TEXT};
  10. print $rec->{SEQUENCE}[0];
  11. $last = pop @ { $rec->{SEQUENCE} };
  12. print $rec->{LOOKUP}{"key"};
  13. ($first_k, $first_v) = each %{ $rec->{LOOKUP} };
  14. $answer = $rec->{THATCODE}->($arg);
  15. $answer = $rec->{THISCODE}->($arg1, $arg2);
  16. # careful of extra block braces on fh ref
  17. print { $rec->{HANDLE} } "a string\n";
  18. use FileHandle;
  19. $rec->{HANDLE}->autoflush(1);
  20. $rec->{HANDLE}->print(" a string\n");

Declaration of a HASH OF COMPLEX RECORDS

  1. %TV = (
  2. flintstones => {
  3. series => "flintstones",
  4. nights => [ qw(monday thursday friday) ],
  5. members => [
  6. { name => "fred", role => "lead", age => 36, },
  7. { name => "wilma", role => "wife", age => 31, },
  8. { name => "pebbles", role => "kid", age => 4, },
  9. ],
  10. },
  11. jetsons => {
  12. series => "jetsons",
  13. nights => [ qw(wednesday saturday) ],
  14. members => [
  15. { name => "george", role => "lead", age => 41, },
  16. { name => "jane", role => "wife", age => 39, },
  17. { name => "elroy", role => "kid", age => 9, },
  18. ],
  19. },
  20. simpsons => {
  21. series => "simpsons",
  22. nights => [ qw(monday) ],
  23. members => [
  24. { name => "homer", role => "lead", age => 34, },
  25. { name => "marge", role => "wife", age => 37, },
  26. { name => "bart", role => "kid", age => 11, },
  27. ],
  28. },
  29. );

Generation of a HASH OF COMPLEX RECORDS

  1. # reading from file
  2. # this is most easily done by having the file itself be
  3. # in the raw data format as shown above. perl is happy
  4. # to parse complex data structures if declared as data, so
  5. # sometimes it's easiest to do that
  6. # here's a piece by piece build up
  7. $rec = {};
  8. $rec->{series} = "flintstones";
  9. $rec->{nights} = [ find_days() ];
  10. @members = ();
  11. # assume this file in field=value syntax
  12. while (<>) {
  13. %fields = split /[\s=]+/;
  14. push @members, { %fields };
  15. }
  16. $rec->{members} = [ @members ];
  17. # now remember the whole thing
  18. $TV{ $rec->{series} } = $rec;
  19. ###########################################################
  20. # now, you might want to make interesting extra fields that
  21. # include pointers back into the same data structure so if
  22. # change one piece, it changes everywhere, like for example
  23. # if you wanted a {kids} field that was a reference
  24. # to an array of the kids' records without having duplicate
  25. # records and thus update problems.
  26. ###########################################################
  27. foreach $family (keys %TV) {
  28. $rec = $TV{$family}; # temp pointer
  29. @kids = ();
  30. for $person ( @{ $rec->{members} } ) {
  31. if ($person->{role} =~ /kid|son|daughter/) {
  32. push @kids, $person;
  33. }
  34. }
  35. # REMEMBER: $rec and $TV{$family} point to same data!!
  36. $rec->{kids} = [ @kids ];
  37. }
  38. # you copied the array, but the array itself contains pointers
  39. # to uncopied objects. this means that if you make bart get
  40. # older via
  41. $TV{simpsons}{kids}[0]{age}++;
  42. # then this would also change in
  43. print $TV{simpsons}{members}[2]{age};
  44. # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2]
  45. # both point to the same underlying anonymous hash table
  46. # print the whole thing
  47. foreach $family ( keys %TV ) {
  48. print "the $family";
  49. print " is on during @{ $TV{$family}{nights} }\n";
  50. print "its members are:\n";
  51. for $who ( @{ $TV{$family}{members} } ) {
  52. print " $who->{name} ($who->{role}), age $who->{age}\n";
  53. }
  54. print "it turns out that $TV{$family}{lead} has ";
  55. print scalar ( @{ $TV{$family}{kids} } ), " kids named ";
  56. print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } );
  57. print "\n";
  58. }

Database Ties

You cannot easily tie a multilevel data structure (such as a hash of hashes) to a dbm file. The first problem is that all but GDBM and Berkeley DB have size limitations, but beyond that, you also have problems with how references are to be represented on disk. One experimental module that does partially attempt to address this need is the MLDBM module. Check your nearest CPAN site as described in perlmodlib for source code to MLDBM.

SEE ALSO

perlref(1), perllol(1), perldata(1), perlobj(1)

AUTHOR

Tom Christiansen <tchrist@perl.com>

Last update: Wed Oct 23 04:57:50 MET DST 1996