2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>perldsc - Perl Data Structures Cookbook
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> perldsc - Perl Data Structures Cookbook
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#description">DESCRIPTION
</a></li>
24 <li><a href=
"#references">REFERENCES
</a></li>
25 <li><a href=
"#common_mistakes">COMMON MISTAKES
</a></li>
26 <li><a href=
"#caveat_on_precedence">CAVEAT ON PRECEDENCE
</a></li>
27 <li><a href=
"#why_you_should_always_use_strict">WHY YOU SHOULD ALWAYS
<code>use strict
</code></a></li>
28 <li><a href=
"#debugging">DEBUGGING
</a></li>
29 <li><a href=
"#code_examples">CODE EXAMPLES
</a></li>
30 <li><a href=
"#arrays_of_arrays">ARRAYS OF ARRAYS
</a></li>
33 <li><a href=
"#declaration_of_an_array_of_arrays">Declaration of an ARRAY OF ARRAYS
</a></li>
34 <li><a href=
"#generation_of_an_array_of_arrays">Generation of an ARRAY OF ARRAYS
</a></li>
35 <li><a href=
"#access_and_printing_of_an_array_of_arrays">Access and Printing of an ARRAY OF ARRAYS
</a></li>
38 <li><a href=
"#hashes_of_arrays">HASHES OF ARRAYS
</a></li>
41 <li><a href=
"#declaration_of_a_hash_of_arrays">Declaration of a HASH OF ARRAYS
</a></li>
42 <li><a href=
"#generation_of_a_hash_of_arrays">Generation of a HASH OF ARRAYS
</a></li>
43 <li><a href=
"#access_and_printing_of_a_hash_of_arrays">Access and Printing of a HASH OF ARRAYS
</a></li>
46 <li><a href=
"#arrays_of_hashes">ARRAYS OF HASHES
</a></li>
49 <li><a href=
"#declaration_of_an_array_of_hashes">Declaration of an ARRAY OF HASHES
</a></li>
50 <li><a href=
"#generation_of_an_array_of_hashes">Generation of an ARRAY OF HASHES
</a></li>
51 <li><a href=
"#access_and_printing_of_an_array_of_hashes">Access and Printing of an ARRAY OF HASHES
</a></li>
54 <li><a href=
"#hashes_of_hashes">HASHES OF HASHES
</a></li>
57 <li><a href=
"#declaration_of_a_hash_of_hashes">Declaration of a HASH OF HASHES
</a></li>
58 <li><a href=
"#generation_of_a_hash_of_hashes">Generation of a HASH OF HASHES
</a></li>
59 <li><a href=
"#access_and_printing_of_a_hash_of_hashes">Access and Printing of a HASH OF HASHES
</a></li>
62 <li><a href=
"#more_elaborate_records">MORE ELABORATE RECORDS
</a></li>
65 <li><a href=
"#declaration_of_more_elaborate_records">Declaration of MORE ELABORATE RECORDS
</a></li>
66 <li><a href=
"#declaration_of_a_hash_of_complex_records">Declaration of a HASH OF COMPLEX RECORDS
</a></li>
67 <li><a href=
"#generation_of_a_hash_of_complex_records">Generation of a HASH OF COMPLEX RECORDS
</a></li>
70 <li><a href=
"#database_ties">Database Ties
</a></li>
71 <li><a href=
"#see_also">SEE ALSO
</a></li>
72 <li><a href=
"#author">AUTHOR
</a></li>
79 <h1><a name=
"name_x_data_structure__x_complex_data_structure__x_struct_">NAME
81 <p>perldsc - Perl Data Structures Cookbook
</p>
85 <h1><a name=
"description">DESCRIPTION
</a></h1>
86 <p>The single feature most sorely lacking in the Perl programming language
87 prior to its
5.0 release was complex data structures. Even without direct
88 language support, some valiant programmers did manage to emulate them, but
89 it was hard work and not for the faint of heart. You could occasionally
90 get away with the
<code>$m{$AoA,$b}
</code> notation borrowed from
<strong>awk
</strong> in which the
91 keys are actually more like a single concatenated string
<code>"$AoA$b
"</code>, but
92 traversal and sorting were difficult. More desperate programmers even
93 hacked Perl's internal symbol table directly, a strategy that proved hard
94 to develop and maintain--to put it mildly.
</p>
95 <p>The
5.0 release of Perl let us have complex data structures. You
96 may now write something like this and all of a sudden, you'd have an array
97 with three dimensions!
</p>
107 <p>Alas, however simple this may appear, underneath it's a much more
108 elaborate construct than meets the eye!
</p>
109 <p>How do you print it out? Why can't you say just
<code>print @AoA
</code>? How do
110 you sort it? How can you pass it to a function or get one of these back
111 from a function? Is it an object? Can you save it to disk to read
112 back later? How do you access whole rows or columns of that matrix? Do
113 all the values have to be numeric?
</p>
114 <p>As you see, it's quite easy to become confused. While some small portion
115 of the blame for this can be attributed to the reference-based
116 implementation, it's really more due to a lack of existing documentation with
117 examples designed for the beginner.
</p>
118 <p>This document is meant to be a detailed but understandable treatment of the
119 many different sorts of data structures you might want to develop. It
120 should also serve as a cookbook of examples. That way, when you need to
121 create one of these complex data structures, you can just pinch, pilfer, or
122 purloin a drop-in example from here.
</p>
123 <p>Let's look at each of these possible constructs in detail. There are separate
124 sections on each of the following:
</p>
126 <li><strong><a name=
"item_arrays_of_arrays">arrays of arrays
</a></strong>
128 <li><strong><a name=
"item_hashes_of_arrays">hashes of arrays
</a></strong>
130 <li><strong><a name=
"item_arrays_of_hashes">arrays of hashes
</a></strong>
132 <li><strong><a name=
"item_hashes_of_hashes">hashes of hashes
</a></strong>
134 <li><strong><a name=
"item_more_elaborate_constructs">more elaborate constructs
</a></strong>
137 <p>But for now, let's look at general issues common to all
138 these types of data structures.
</p>
142 <h1><a name=
"references_x_reference__x_dereference__x_dereferencing__x_pointer_">REFERENCES
144 <p>The most important thing to understand about all data structures in Perl
145 -- including multidimensional arrays--is that even though they might
146 appear otherwise, Perl
<code>@ARRAY
</code>s and
<code>%HASH
</code>es are all internally
147 one-dimensional. They can hold only scalar values (meaning a string,
148 number, or a reference). They cannot directly contain other arrays or
149 hashes, but instead contain
<em>references
</em> to other arrays or hashes.
151 <p>You can't use a reference to an array or hash in quite the same way that you
152 would a real array or hash. For C or C++ programmers unused to
153 distinguishing between arrays and pointers to the same, this can be
154 confusing. If so, just think of it as the difference between a structure
155 and a pointer to a structure.
</p>
156 <p>You can (and should) read more about references in the
<code>perlref(
1)
</code> man
157 page. Briefly, references are rather like pointers that know what they
158 point to. (Objects are also a kind of reference, but we won't be needing
159 them right away--if ever.) This means that when you have something which
160 looks to you like an access to a two-or-more-dimensional array and/or hash,
161 what's really going on is that the base type is
162 merely a one-dimensional entity that contains references to the next
163 level. It's just that you can
<em>use
</em> it as though it were a
164 two-dimensional one. This is actually the way almost all C
165 multidimensional arrays work as well.
</p>
167 $array[
7][
12] # array of arrays
168 $array[
7]{string} # array of hashes
169 $hash{string}[
7] # hash of arrays
170 $hash{string}{'another string'} # hash of hashes
</pre>
171 <p>Now, because the top level contains only references, if you try to print
172 out your array in with a simple
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print()
</code></a> function, you'll get something
173 that doesn't look very nice, like this:
</p>
175 @AoA = ( [
2,
3], [
4,
5,
7], [
0] );
179 ARRAY(
0x83c38)ARRAY(
0x8b194)ARRAY(
0x8b1d0)
</pre>
180 <p>That's because Perl doesn't (ever) implicitly dereference your variables.
181 If you want to get at the thing a reference is referring to, then you have
182 to do this yourself using either prefix typing indicators, like
183 <code>${$blah}
</code>,
<code>@{$blah}
</code>,
<code>@{$blah[$i]}
</code>, or else postfix pointer arrows,
184 like
<code>$a-
>[
3]
</code>,
<code>$h-
>{fred}
</code>, or even
<code>$ob-
>method()-
>[
3]
</code>.
</p>
188 <h1><a name=
"common_mistakes">COMMON MISTAKES
</a></h1>
189 <p>The two most common mistakes made in constructing something like
190 an array of arrays is either accidentally counting the number of
191 elements or else taking a reference to the same memory location
192 repeatedly. Here's the case where you just get the count instead
193 of a nested array:
</p>
196 @array = somefunc($i);
197 $AoA[$i] = @array; # WRONG!
199 <p>That's just the simple case of assigning an array to a scalar and getting
200 its element count. If that's what you really and truly want, then you
201 might do well to consider being a tad more explicit about it, like this:
</p>
204 @array = somefunc($i);
205 $counts[$i] = scalar @array;
207 <p>Here's the case of taking a reference to the same memory location
211 @array = somefunc($i);
212 $AoA[$i] = \@array; # WRONG!
214 <p>So, what's the big problem with that? It looks right, doesn't it?
215 After all, I just told you that you need an array of references, so by
216 golly, you've made me one!
</p>
217 <p>Unfortunately, while this is true, it's still broken. All the references
218 in @AoA refer to the
<em>very same place
</em>, and they will therefore all hold
219 whatever was last in @array! It's similar to the problem demonstrated in
220 the following C program:
</p>
222 #include
<pwd.h
>
224 struct passwd *getpwnam(), *rp, *dp;
225 rp = getpwnam(
"root
");
226 dp = getpwnam(
"daemon
");
</pre>
228 printf(
"daemon name is %s\nroot name is %s\n
",
229 dp-
>pw_name, rp-
>pw_name);
231 <p>Which will print
</p>
233 daemon name is daemon
234 root name is daemon
</pre>
235 <p>The problem is that both
<code>rp
</code> and
<code>dp
</code> are pointers to the same location
236 in memory! In C, you'd have to remember to
<code>malloc()
</code> yourself some new
237 memory. In Perl, you'll want to use the array constructor
<code>[]
</code> or the
238 hash constructor
<code>{}
</code> instead. Here's the right way to do the preceding
239 broken code fragments:
243 @array = somefunc($i);
244 $AoA[$i] = [ @array ];
246 <p>The square brackets make a reference to a new array with a
<em>copy
</em>
247 of what's in @array at the time of the assignment. This is what
249 <p>Note that this will produce something similar, but it's
250 much harder to read:
</p>
254 @{$AoA[$i]} = @array;
256 <p>Is it the same? Well, maybe so--and maybe not. The subtle difference
257 is that when you assign something in square brackets, you know for sure
258 it's always a brand new reference with a new
<em>copy
</em> of the data.
259 Something else could be going on in this new case with the
<code>@{$AoA[$i]}}
</code>
260 dereference on the left-hand-side of the assignment. It all depends on
261 whether
<code>$AoA[$i]
</code> had been undefined to start with, or whether it
262 already contained a reference. If you had already populated @AoA with
263 references, as in
</p>
265 $AoA[
3] = \@another_array;
</pre>
266 <p>Then the assignment with the indirection on the left-hand-side would
267 use the existing reference that was already there:
</p>
269 @{$AoA[
3]} = @array;
</pre>
270 <p>Of course, this
<em>would
</em> have the ``interesting'' effect of clobbering
271 @another_array. (Have you ever noticed how when a programmer says
272 something is ``interesting'', that rather than meaning ``intriguing'',
273 they're disturbingly more apt to mean that it's ``annoying'',
274 ``difficult'', or both? :-)
</p>
275 <p>So just remember always to use the array or hash constructors with
<code>[]
</code>
276 or
<code>{}
</code>, and you'll be fine, although it's not always optimally
278 <p>Surprisingly, the following dangerous-looking construct will
279 actually work out fine:
</p>
282 my @array = somefunc($i);
285 <p>That's because
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my()
</code></a> is more of a run-time statement than it is a
286 compile-time declaration
<em>per se
</em>. This means that the
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my()
</code></a> variable is
287 remade afresh each time through the loop. So even though it
<em>looks
</em> as
288 though you stored the same variable reference each time, you actually did
289 not! This is a subtle distinction that can produce more efficient code at
290 the risk of misleading all but the most experienced of programmers. So I
291 usually advise against teaching it to beginners. In fact, except for
292 passing arguments to functions, I seldom like to see the gimme-a-reference
293 operator (backslash) used much at all in code. Instead, I advise
294 beginners that they (and most of the rest of us) should try to use the
295 much more easily understood constructors
<code>[]
</code> and
<code>{}
</code> instead of
296 relying upon lexical (or dynamic) scoping and hidden reference-counting to
297 do the right thing behind the scenes.
</p>
300 $AoA[$i] = [ @array ]; # usually best
301 $AoA[$i] = \@array; # perilous; just how my() was that array?
302 @{ $AoA[$i] } = @array; # way too tricky for most programmers
</pre>
306 <h1><a name=
"caveat_on_precedence_x_dereference__precedence__x_dereferencing__precedence_">CAVEAT ON PRECEDENCE
308 <p>Speaking of things like
<code>@{$AoA[$i]}
</code>, the following are actually the
312 $aref-
>[
2][
2] # clear
313 $$aref[
2][
2] # confusing
</pre>
314 <p>That's because Perl's precedence rules on its five prefix dereferencers
315 (which look like someone swearing:
<code>$ @ * %
&</code>) make them bind more
316 tightly than the postfix subscripting brackets or braces! This will no
317 doubt come as a great shock to the C or C++ programmer, who is quite
318 accustomed to using
<code>*a[i]
</code> to mean what's pointed to by the
<em>i'th
</em>
319 element of
<code>a
</code>. That is, they first take the subscript, and only then
320 dereference the thing at that subscript. That's fine in C, but this isn't C.
</p>
321 <p>The seemingly equivalent construct in Perl,
<code>$$aref[$i]
</code> first does
322 the deref of $aref, making it take $aref as a reference to an
323 array, and then dereference that, and finally tell you the
<em>i'th
</em> value
324 of the array pointed to by $AoA. If you wanted the C notion, you'd have to
325 write
<code>${$AoA[$i]}
</code> to force the
<code>$AoA[$i]
</code> to get evaluated first
326 before the leading
<code>$
</code> dereferencer.
</p>
330 <h1><a name=
"why_you_should_always_use_strict">WHY YOU SHOULD ALWAYS
<code>use strict
</code></a></h1>
331 <p>If this is starting to sound scarier than it's worth, relax. Perl has
332 some features to help you avoid its most common pitfalls. The best
333 way to avoid getting confused is to start every program like this:
</p>
337 <p>This way, you'll be forced to declare all your variables with
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my()
</code></a> and
338 also disallow accidental ``symbolic dereferencing''. Therefore if you'd done
342 [
"fred
",
"barney
",
"pebbles
",
"bambam
",
"dino
", ],
343 [
"homer
",
"bart
",
"marge
",
"maggie
", ],
344 [
"george
",
"jane
",
"elroy
",
"judy
", ],
347 print $aref[
2][
2];
</pre>
348 <p>The compiler would immediately flag that as an error
<em>at compile time
</em>,
349 because you were accidentally accessing
<code>@aref
</code>, an undeclared
350 variable, and it would thereby remind you to write instead:
</p>
352 print $aref-
>[
2][
2]
</pre>
356 <h1><a name=
"debugging_x_data_structure__debugging__x_complex_data_structure__debugging__x_aoa__debugging__x_hoa__debugging__x_aoh__debugging__x_hoh__debugging__x_array_of_arrays__debugging__x_hash_of_arrays__debugging__x_array_of_hashes__debugging__x_hash_of_hashes__debugging_">DEBUGGING
361 <p>Before version
5.002, the standard Perl debugger didn't do a very nice job of
362 printing out complex data structures. With
5.002 or above, the
363 debugger includes several new features, including command line editing as
364 well as the
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html#item_x"><code>x
</code></a> command to dump out complex data structures. For
365 example, given the assignment to $AoA above, here's the debugger output:
</p>
368 $AoA = ARRAY(
0x13b5a0)
388 <h1><a name=
"code_examples">CODE EXAMPLES
</a></h1>
389 <p>Presented with little comment (these will get their own manpages someday)
390 here are short code examples illustrating access of various
391 types of data structures.
</p>
395 <h1><a name=
"arrays_of_arrays_x_array_of_arrays__x_aoa_">ARRAYS OF ARRAYS
399 <h2><a name=
"declaration_of_an_array_of_arrays">Declaration of an ARRAY OF ARRAYS
</a></h2>
402 [
"fred
",
"barney
" ],
403 [
"george
",
"jane
",
"elroy
" ],
404 [
"homer
",
"marge
",
"bart
" ],
408 <h2><a name=
"generation_of_an_array_of_arrays">Generation of an ARRAY OF ARRAYS
</a></h2>
412 push @AoA, [ split ];
417 $AoA[$i] = [ somefunc($i) ];
426 # add to an existing row
427 push @{ $AoA[
0] },
"wilma
",
"betty
";
</pre>
430 <h2><a name=
"access_and_printing_of_an_array_of_arrays">Access and Printing of an ARRAY OF ARRAYS
</a></h2>
433 $AoA[
0][
0] =
"Fred
";
</pre>
436 $AoA[
1][
1] =~ s/(\w)/\u$
1/;
</pre>
438 # print the whole thing with refs
440 print
"\t [ @$aref ],\n
";
443 # print the whole thing with indices
444 for $i (
0 .. $#AoA ) {
445 print
"\t [ @{$AoA[$i]} ],\n
";
448 # print the whole thing one at a time
449 for $i (
0 .. $#AoA ) {
450 for $j (
0 .. $#{ $AoA[$i] } ) {
451 print
"elt $i $j is $AoA[$i][$j]\n
";
457 <h1><a name=
"hashes_of_arrays_x_hash_of_arrays__x_hoa_">HASHES OF ARRAYS
461 <h2><a name=
"declaration_of_a_hash_of_arrays">Declaration of a HASH OF ARRAYS
</a></h2>
464 flintstones =
> [
"fred
",
"barney
" ],
465 jetsons =
> [
"george
",
"jane
",
"elroy
" ],
466 simpsons =
> [
"homer
",
"marge
",
"bart
" ],
470 <h2><a name=
"generation_of_a_hash_of_arrays">Generation of a HASH OF ARRAYS
</a></h2>
473 # flintstones: fred barney wilma dino
475 next unless s/^(.*?):\s*//;
476 $HoA{$
1} = [ split ];
479 # reading from file; more temps
480 # flintstones: fred barney wilma dino
481 while ( $line =
<> ) {
482 ($who, $rest) = split /:\s*/, $line,
2;
483 @fields = split ' ', $rest;
484 $HoA{$who} = [ @fields ];
487 # calling a function that returns a list
488 for $group (
"simpsons
",
"jetsons
",
"flintstones
" ) {
489 $HoA{$group} = [ get_family($group) ];
492 # likewise, but using temps
493 for $group (
"simpsons
",
"jetsons
",
"flintstones
" ) {
494 @members = get_family($group);
495 $HoA{$group} = [ @members ];
498 # append new members to an existing family
499 push @{ $HoA{
"flintstones
"} },
"wilma
",
"betty
";
</pre>
502 <h2><a name=
"access_and_printing_of_a_hash_of_arrays">Access and Printing of a HASH OF ARRAYS
</a></h2>
505 $HoA{flintstones}[
0] =
"Fred
";
</pre>
508 $HoA{simpsons}[
1] =~ s/(\w)/\u$
1/;
</pre>
510 # print the whole thing
511 foreach $family ( keys %HoA ) {
512 print
"$family: @{ $HoA{$family} }\n
"
515 # print the whole thing with indices
516 foreach $family ( keys %HoA ) {
517 print
"family:
";
518 foreach $i (
0 .. $#{ $HoA{$family} } ) {
519 print
" $i = $HoA{$family}[$i]
";
521 print
"\n
";
524 # print the whole thing sorted by number of members
525 foreach $family ( sort { @{$HoA{$b}}
<=
> @{$HoA{$a}} } keys %HoA ) {
526 print
"$family: @{ $HoA{$family} }\n
"
529 # print the whole thing sorted by number of members and name
530 foreach $family ( sort {
531 @{$HoA{$b}}
<=
> @{$HoA{$a}}
536 print
"$family:
", join(
",
", sort @{ $HoA{$family} }),
"\n
";
541 <h1><a name=
"arrays_of_hashes_x_array_of_hashes__x_aoh_">ARRAYS OF HASHES
545 <h2><a name=
"declaration_of_an_array_of_hashes">Declaration of an ARRAY OF HASHES
</a></h2>
549 Lead =
> "fred
",
550 Friend =
> "barney
",
553 Lead =
> "george
",
554 Wife =
> "jane
",
555 Son =
> "elroy
",
558 Lead =
> "homer
",
559 Wife =
> "marge
",
560 Son =
> "bart
",
565 <h2><a name=
"generation_of_an_array_of_hashes">Generation of an ARRAY OF HASHES
</a></h2>
568 # format: LEAD=fred FRIEND=barney
571 for $field ( split ) {
572 ($key, $value) = split /=/, $field;
573 $rec-
>{$key} = $value;
579 # format: LEAD=fred FRIEND=barney
582 push @AoH, { split /[\s+=]/ };
585 # calling a function that returns a key/value pair list, like
586 #
"lead
",
"fred
",
"daughter
",
"pebbles
"
587 while ( %fields = getnextpairset() ) {
588 push @AoH, { %fields };
591 # likewise, but using no temp vars
593 push @AoH, { parsepairs($_) };
596 # add key/value to an element
597 $AoH[
0]{pet} =
"dino
";
598 $AoH[
2]{pet} =
"santa's little helper
";
</pre>
601 <h2><a name=
"access_and_printing_of_an_array_of_hashes">Access and Printing of an ARRAY OF HASHES
</a></h2>
604 $AoH[
0]{lead} =
"fred
";
</pre>
607 $AoH[
1]{lead} =~ s/(\w)/\u$
1/;
</pre>
609 # print the whole thing with refs
611 print
"{
";
612 for $role ( keys %$href ) {
613 print
"$role=$href-
>{$role}
";
615 print
"}\n
";
618 # print the whole thing with indices
619 for $i (
0 .. $#AoH ) {
620 print
"$i is {
";
621 for $role ( keys %{ $AoH[$i] } ) {
622 print
"$role=$AoH[$i]{$role}
";
624 print
"}\n
";
627 # print the whole thing one at a time
628 for $i (
0 .. $#AoH ) {
629 for $role ( keys %{ $AoH[$i] } ) {
630 print
"elt $i $role is $AoH[$i]{$role}\n
";
636 <h1><a name=
"hashes_of_hashes_x_hass_of_hashes__x_hoh_">HASHES OF HASHES
640 <h2><a name=
"declaration_of_a_hash_of_hashes">Declaration of a HASH OF HASHES
</a></h2>
644 lead =
> "fred
",
645 pal =
> "barney
",
648 lead =
> "george
",
649 wife =
> "jane
",
650 "his boy
" =
> "elroy
",
653 lead =
> "homer
",
654 wife =
> "marge
",
655 kid =
> "bart
",
660 <h2><a name=
"generation_of_a_hash_of_hashes">Generation of a HASH OF HASHES
</a></h2>
663 # flintstones: lead=fred pal=barney wife=wilma pet=dino
665 next unless s/^(.*?):\s*//;
667 for $field ( split ) {
668 ($key, $value) = split /=/, $field;
669 $HoH{$who}{$key} = $value;
672 # reading from file; more temps
674 next unless s/^(.*?):\s*//;
678 for $field ( split ) {
679 ($key, $value) = split /=/, $field;
680 $rec-
>{$key} = $value;
684 # calling a function that returns a key,value hash
685 for $group (
"simpsons
",
"jetsons
",
"flintstones
" ) {
686 $HoH{$group} = { get_family($group) };
689 # likewise, but using temps
690 for $group (
"simpsons
",
"jetsons
",
"flintstones
" ) {
691 %members = get_family($group);
692 $HoH{$group} = { %members };
695 # append new members to an existing family
697 wife =
> "wilma
",
698 pet =
> "dino
",
701 for $what (keys %new_folks) {
702 $HoH{flintstones}{$what} = $new_folks{$what};
706 <h2><a name=
"access_and_printing_of_a_hash_of_hashes">Access and Printing of a HASH OF HASHES
</a></h2>
709 $HoH{flintstones}{wife} =
"wilma
";
</pre>
712 $HoH{simpsons}{lead} =~ s/(\w)/\u$
1/;
</pre>
714 # print the whole thing
715 foreach $family ( keys %HoH ) {
716 print
"$family: {
";
717 for $role ( keys %{ $HoH{$family} } ) {
718 print
"$role=$HoH{$family}{$role}
";
720 print
"}\n
";
723 # print the whole thing somewhat sorted
724 foreach $family ( sort keys %HoH ) {
725 print
"$family: {
";
726 for $role ( sort keys %{ $HoH{$family} } ) {
727 print
"$role=$HoH{$family}{$role}
";
729 print
"}\n
";
732 # print the whole thing sorted by number of members
733 foreach $family ( sort { keys %{$HoH{$b}}
<=
> keys %{$HoH{$a}} } keys %HoH ) {
734 print
"$family: {
";
735 for $role ( sort keys %{ $HoH{$family} } ) {
736 print
"$role=$HoH{$family}{$role}
";
738 print
"}\n
";
741 # establish a sort order (rank) for each role
743 for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }
</pre>
745 # now print the whole thing sorted by number of members
746 foreach $family ( sort { keys %{ $HoH{$b} }
<=
> keys %{ $HoH{$a} } } keys %HoH ) {
747 print
"$family: {
";
748 # and print these according to rank order
749 for $role ( sort { $rank{$a}
<=
> $rank{$b} } keys %{ $HoH{$family} } ) {
750 print
"$role=$HoH{$family}{$role}
";
752 print
"}\n
";
757 <h1><a name=
"more_elaborate_records_x_record__x_structure__x_struct_">MORE ELABORATE RECORDS
761 <h2><a name=
"declaration_of_more_elaborate_records">Declaration of MORE ELABORATE RECORDS
</a></h2>
762 <p>Here's a sample showing how to create and use a record whose fields are of
763 many different sorts:
</p>
767 SEQUENCE =
> [ @old_values ],
768 LOOKUP =
> { %some_table },
769 THATCODE =
> \
&some_function,
770 THISCODE =
> sub { $_[
0] ** $_[
1] },
771 HANDLE =
> \*STDOUT,
774 print $rec-
>{TEXT};
</pre>
776 print $rec-
>{SEQUENCE}[
0];
777 $last = pop @ { $rec-
>{SEQUENCE} };
</pre>
779 print $rec-
>{LOOKUP}{
"key
"};
780 ($first_k, $first_v) = each %{ $rec-
>{LOOKUP} };
</pre>
782 $answer = $rec-
>{THATCODE}-
>($arg);
783 $answer = $rec-
>{THISCODE}-
>($arg1, $arg2);
</pre>
785 # careful of extra block braces on fh ref
786 print { $rec-
>{HANDLE} }
"a string\n
";
</pre>
789 $rec-
>{HANDLE}-
>autoflush(
1);
790 $rec-
>{HANDLE}-
>print(
" a string\n
");
</pre>
793 <h2><a name=
"declaration_of_a_hash_of_complex_records">Declaration of a HASH OF COMPLEX RECORDS
</a></h2>
797 series =
> "flintstones
",
798 nights =
> [ qw(monday thursday friday) ],
800 { name =
> "fred
", role =
> "lead
", age =
> 36, },
801 { name =
> "wilma
", role =
> "wife
", age =
> 31, },
802 { name =
> "pebbles
", role =
> "kid
", age =
> 4, },
807 series =
> "jetsons
",
808 nights =
> [ qw(wednesday saturday) ],
810 { name =
> "george
", role =
> "lead
", age =
> 41, },
811 { name =
> "jane
", role =
> "wife
", age =
> 39, },
812 { name =
> "elroy
", role =
> "kid
", age =
> 9, },
817 series =
> "simpsons
",
818 nights =
> [ qw(monday) ],
820 { name =
> "homer
", role =
> "lead
", age =
> 34, },
821 { name =
> "marge
", role =
> "wife
", age =
> 37, },
822 { name =
> "bart
", role =
> "kid
", age =
> 11, },
828 <h2><a name=
"generation_of_a_hash_of_complex_records">Generation of a HASH OF COMPLEX RECORDS
</a></h2>
831 # this is most easily done by having the file itself be
832 # in the raw data format as shown above. perl is happy
833 # to parse complex data structures if declared as data, so
834 # sometimes it's easiest to do that
</pre>
836 # here's a piece by piece build up
838 $rec-
>{series} =
"flintstones
";
839 $rec-
>{nights} = [ find_days() ];
</pre>
842 # assume this file in field=value syntax
844 %fields = split /[\s=]+/;
845 push @members, { %fields };
847 $rec-
>{members} = [ @members ];
</pre>
849 # now remember the whole thing
850 $TV{ $rec-
>{series} } = $rec;
</pre>
852 ###########################################################
853 # now, you might want to make interesting extra fields that
854 # include pointers back into the same data structure so if
855 # change one piece, it changes everywhere, like for example
856 # if you wanted a {kids} field that was a reference
857 # to an array of the kids' records without having duplicate
858 # records and thus update problems.
859 ###########################################################
860 foreach $family (keys %TV) {
861 $rec = $TV{$family}; # temp pointer
863 for $person ( @{ $rec-
>{members} } ) {
864 if ($person-
>{role} =~ /kid|son|daughter/) {
868 # REMEMBER: $rec and $TV{$family} point to same data!!
869 $rec-
>{kids} = [ @kids ];
872 # you copied the array, but the array itself contains pointers
873 # to uncopied objects. this means that if you make bart get
876 $TV{simpsons}{kids}[
0]{age}++;
</pre>
878 # then this would also change in
879 print $TV{simpsons}{members}[
2]{age};
</pre>
881 # because $TV{simpsons}{kids}[
0] and $TV{simpsons}{members}[
2]
882 # both point to the same underlying anonymous hash table
</pre>
884 # print the whole thing
885 foreach $family ( keys %TV ) {
886 print
"the $family
";
887 print
" is on during @{ $TV{$family}{nights} }\n
";
888 print
"its members are:\n
";
889 for $who ( @{ $TV{$family}{members} } ) {
890 print
" $who-
>{name} ($who-
>{role}), age $who-
>{age}\n
";
892 print
"it turns out that $TV{$family}{lead} has
";
893 print scalar ( @{ $TV{$family}{kids} } ),
" kids named
";
894 print join (
",
", map { $_-
>{name} } @{ $TV{$family}{kids} } );
895 print
"\n
";
900 <h1><a name=
"database_ties">Database Ties
</a></h1>
901 <p>You cannot easily tie a multilevel data structure (such as a hash of
902 hashes) to a dbm file. The first problem is that all but GDBM and
903 Berkeley DB have size limitations, but beyond that, you also have problems
904 with how references are to be represented on disk. One experimental
905 module that does partially attempt to address this need is the MLDBM
906 module. Check your nearest CPAN site as described in
<a href=
"file://C|\msysgit\mingw\html/pod/perlmodlib.html">the perlmodlib manpage
</a> for
907 source code to MLDBM.
</p>
911 <h1><a name=
"see_also">SEE ALSO
</a></h1>
912 <p>perlref(
1), perllol(
1), perldata(
1),
<code>perlobj(
1)
</code></p>
916 <h1><a name=
"author">AUTHOR
</a></h1>
917 <p>Tom Christiansen
<<em><a href=
"mailto:tchrist@perl.com">tchrist@perl.com
</a></em>></p>
919 Wed Oct
23 04:
57:
50 MET DST
1996</p>
920 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
921 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
922 <big><strong><span class=
"block"> perldsc - Perl Data Structures Cookbook
</span></strong></big>