Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perldsc.html
blob278bd95b4095bee5d4a3b9112732cc6499222602
1 <?xml version="1.0" ?>
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">
4 <head>
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:" />
8 </head>
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">&nbsp;perldsc - Perl Data Structures Cookbook</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
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>
31 <ul>
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>
36 </ul>
38 <li><a href="#hashes_of_arrays">HASHES OF ARRAYS</a></li>
39 <ul>
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>
44 </ul>
46 <li><a href="#arrays_of_hashes">ARRAYS OF HASHES</a></li>
47 <ul>
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>
52 </ul>
54 <li><a href="#hashes_of_hashes">HASHES OF HASHES</a></li>
55 <ul>
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>
60 </ul>
62 <li><a href="#more_elaborate_records">MORE ELABORATE RECORDS</a></li>
63 <ul>
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>
68 </ul>
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>
73 </ul>
74 <!-- INDEX END -->
76 <hr />
77 <p>
78 </p>
79 <h1><a name="name_x_data_structure__x_complex_data_structure__x_struct_">NAME
80 </a></h1>
81 <p>perldsc - Perl Data Structures Cookbook</p>
82 <p>
83 </p>
84 <hr />
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>&quot;$AoA$b&quot;</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>
98 <pre>
99 for $x (1 .. 10) {
100 for $y (1 .. 10) {
101 for $z (1 .. 10) {
102 $AoA[$x][$y][$z] =
103 $x ** $y + $z;
106 }</pre>
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>
125 <ul>
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>
136 </ul>
137 <p>But for now, let's look at general issues common to all
138 these types of data structures.</p>
140 </p>
141 <hr />
142 <h1><a name="references_x_reference__x_dereference__x_dereferencing__x_pointer_">REFERENCES
143 </a></h1>
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.
150 </p>
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>
166 <pre>
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>
174 <pre>
175 @AoA = ( [2, 3], [4, 5, 7], [0] );
176 print $AoA[1][2];
178 print @AoA;
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-&gt;[3]</code>, <code>$h-&gt;{fred}</code>, or even <code>$ob-&gt;method()-&gt;[3]</code>.</p>
186 </p>
187 <hr />
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>
194 <pre>
195 for $i (1..10) {
196 @array = somefunc($i);
197 $AoA[$i] = @array; # WRONG!
198 }</pre>
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>
202 <pre>
203 for $i (1..10) {
204 @array = somefunc($i);
205 $counts[$i] = scalar @array;
206 }</pre>
207 <p>Here's the case of taking a reference to the same memory location
208 again and again:</p>
209 <pre>
210 for $i (1..10) {
211 @array = somefunc($i);
212 $AoA[$i] = \@array; # WRONG!
213 }</pre>
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>
221 <pre>
222 #include &lt;pwd.h&gt;
223 main() {
224 struct passwd *getpwnam(), *rp, *dp;
225 rp = getpwnam(&quot;root&quot;);
226 dp = getpwnam(&quot;daemon&quot;);</pre>
227 <pre>
228 printf(&quot;daemon name is %s\nroot name is %s\n&quot;,
229 dp-&gt;pw_name, rp-&gt;pw_name);
230 }</pre>
231 <p>Which will print</p>
232 <pre>
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:
240 </p>
241 <pre>
242 for $i (1..10) {
243 @array = somefunc($i);
244 $AoA[$i] = [ @array ];
245 }</pre>
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
248 you want.</p>
249 <p>Note that this will produce something similar, but it's
250 much harder to read:</p>
251 <pre>
252 for $i (1..10) {
253 @array = 0 .. $i;
254 @{$AoA[$i]} = @array;
255 }</pre>
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>
264 <pre>
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>
268 <pre>
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
277 efficient.</p>
278 <p>Surprisingly, the following dangerous-looking construct will
279 actually work out fine:</p>
280 <pre>
281 for $i (1..10) {
282 my @array = somefunc($i);
283 $AoA[$i] = \@array;
284 }</pre>
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>
298 <p>In summary:</p>
299 <pre>
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>
304 </p>
305 <hr />
306 <h1><a name="caveat_on_precedence_x_dereference__precedence__x_dereferencing__precedence_">CAVEAT ON PRECEDENCE
307 </a></h1>
308 <p>Speaking of things like <code>@{$AoA[$i]}</code>, the following are actually the
309 same thing:
310 &gt;&gt;</p>
311 <pre>
312 $aref-&gt;[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>$ @ * % &amp;</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>
328 </p>
329 <hr />
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>
334 <pre>
335 #!/usr/bin/perl -w
336 use strict;</pre>
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
339 this:</p>
340 <pre>
341 my $aref = [
342 [ &quot;fred&quot;, &quot;barney&quot;, &quot;pebbles&quot;, &quot;bambam&quot;, &quot;dino&quot;, ],
343 [ &quot;homer&quot;, &quot;bart&quot;, &quot;marge&quot;, &quot;maggie&quot;, ],
344 [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot;, &quot;judy&quot;, ],
345 ];</pre>
346 <pre>
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>
351 <pre>
352 print $aref-&gt;[2][2]</pre>
354 </p>
355 <hr />
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
360 </a></h1>
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>
366 <pre>
367 DB&lt;1&gt; x $AoA
368 $AoA = ARRAY(0x13b5a0)
369 0 ARRAY(0x1f0a24)
370 0 'fred'
371 1 'barney'
372 2 'pebbles'
373 3 'bambam'
374 4 'dino'
375 1 ARRAY(0x13b558)
376 0 'homer'
377 1 'bart'
378 2 'marge'
379 3 'maggie'
380 2 ARRAY(0x13b540)
381 0 'george'
382 1 'jane'
383 2 'elroy'
384 3 'judy'</pre>
386 </p>
387 <hr />
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>
393 </p>
394 <hr />
395 <h1><a name="arrays_of_arrays_x_array_of_arrays__x_aoa_">ARRAYS OF ARRAYS
396 </a></h1>
398 </p>
399 <h2><a name="declaration_of_an_array_of_arrays">Declaration of an ARRAY OF ARRAYS</a></h2>
400 <pre>
401 @AoA = (
402 [ &quot;fred&quot;, &quot;barney&quot; ],
403 [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot; ],
404 [ &quot;homer&quot;, &quot;marge&quot;, &quot;bart&quot; ],
405 );</pre>
407 </p>
408 <h2><a name="generation_of_an_array_of_arrays">Generation of an ARRAY OF ARRAYS</a></h2>
409 <pre>
410 # reading from file
411 while ( &lt;&gt; ) {
412 push @AoA, [ split ];
413 }</pre>
414 <pre>
415 # calling a function
416 for $i ( 1 .. 10 ) {
417 $AoA[$i] = [ somefunc($i) ];
418 }</pre>
419 <pre>
420 # using temp vars
421 for $i ( 1 .. 10 ) {
422 @tmp = somefunc($i);
423 $AoA[$i] = [ @tmp ];
424 }</pre>
425 <pre>
426 # add to an existing row
427 push @{ $AoA[0] }, &quot;wilma&quot;, &quot;betty&quot;;</pre>
429 </p>
430 <h2><a name="access_and_printing_of_an_array_of_arrays">Access and Printing of an ARRAY OF ARRAYS</a></h2>
431 <pre>
432 # one element
433 $AoA[0][0] = &quot;Fred&quot;;</pre>
434 <pre>
435 # another element
436 $AoA[1][1] =~ s/(\w)/\u$1/;</pre>
437 <pre>
438 # print the whole thing with refs
439 for $aref ( @AoA ) {
440 print &quot;\t [ @$aref ],\n&quot;;
441 }</pre>
442 <pre>
443 # print the whole thing with indices
444 for $i ( 0 .. $#AoA ) {
445 print &quot;\t [ @{$AoA[$i]} ],\n&quot;;
446 }</pre>
447 <pre>
448 # print the whole thing one at a time
449 for $i ( 0 .. $#AoA ) {
450 for $j ( 0 .. $#{ $AoA[$i] } ) {
451 print &quot;elt $i $j is $AoA[$i][$j]\n&quot;;
453 }</pre>
455 </p>
456 <hr />
457 <h1><a name="hashes_of_arrays_x_hash_of_arrays__x_hoa_">HASHES OF ARRAYS
458 </a></h1>
460 </p>
461 <h2><a name="declaration_of_a_hash_of_arrays">Declaration of a HASH OF ARRAYS</a></h2>
462 <pre>
463 %HoA = (
464 flintstones =&gt; [ &quot;fred&quot;, &quot;barney&quot; ],
465 jetsons =&gt; [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot; ],
466 simpsons =&gt; [ &quot;homer&quot;, &quot;marge&quot;, &quot;bart&quot; ],
467 );</pre>
469 </p>
470 <h2><a name="generation_of_a_hash_of_arrays">Generation of a HASH OF ARRAYS</a></h2>
471 <pre>
472 # reading from file
473 # flintstones: fred barney wilma dino
474 while ( &lt;&gt; ) {
475 next unless s/^(.*?):\s*//;
476 $HoA{$1} = [ split ];
477 }</pre>
478 <pre>
479 # reading from file; more temps
480 # flintstones: fred barney wilma dino
481 while ( $line = &lt;&gt; ) {
482 ($who, $rest) = split /:\s*/, $line, 2;
483 @fields = split ' ', $rest;
484 $HoA{$who} = [ @fields ];
485 }</pre>
486 <pre>
487 # calling a function that returns a list
488 for $group ( &quot;simpsons&quot;, &quot;jetsons&quot;, &quot;flintstones&quot; ) {
489 $HoA{$group} = [ get_family($group) ];
490 }</pre>
491 <pre>
492 # likewise, but using temps
493 for $group ( &quot;simpsons&quot;, &quot;jetsons&quot;, &quot;flintstones&quot; ) {
494 @members = get_family($group);
495 $HoA{$group} = [ @members ];
496 }</pre>
497 <pre>
498 # append new members to an existing family
499 push @{ $HoA{&quot;flintstones&quot;} }, &quot;wilma&quot;, &quot;betty&quot;;</pre>
501 </p>
502 <h2><a name="access_and_printing_of_a_hash_of_arrays">Access and Printing of a HASH OF ARRAYS</a></h2>
503 <pre>
504 # one element
505 $HoA{flintstones}[0] = &quot;Fred&quot;;</pre>
506 <pre>
507 # another element
508 $HoA{simpsons}[1] =~ s/(\w)/\u$1/;</pre>
509 <pre>
510 # print the whole thing
511 foreach $family ( keys %HoA ) {
512 print &quot;$family: @{ $HoA{$family} }\n&quot;
513 }</pre>
514 <pre>
515 # print the whole thing with indices
516 foreach $family ( keys %HoA ) {
517 print &quot;family: &quot;;
518 foreach $i ( 0 .. $#{ $HoA{$family} } ) {
519 print &quot; $i = $HoA{$family}[$i]&quot;;
521 print &quot;\n&quot;;
522 }</pre>
523 <pre>
524 # print the whole thing sorted by number of members
525 foreach $family ( sort { @{$HoA{$b}} &lt;=&gt; @{$HoA{$a}} } keys %HoA ) {
526 print &quot;$family: @{ $HoA{$family} }\n&quot;
527 }</pre>
528 <pre>
529 # print the whole thing sorted by number of members and name
530 foreach $family ( sort {
531 @{$HoA{$b}} &lt;=&gt; @{$HoA{$a}}
533 $a cmp $b
534 } keys %HoA )
536 print &quot;$family: &quot;, join(&quot;, &quot;, sort @{ $HoA{$family} }), &quot;\n&quot;;
537 }</pre>
539 </p>
540 <hr />
541 <h1><a name="arrays_of_hashes_x_array_of_hashes__x_aoh_">ARRAYS OF HASHES
542 </a></h1>
544 </p>
545 <h2><a name="declaration_of_an_array_of_hashes">Declaration of an ARRAY OF HASHES</a></h2>
546 <pre>
547 @AoH = (
549 Lead =&gt; &quot;fred&quot;,
550 Friend =&gt; &quot;barney&quot;,
553 Lead =&gt; &quot;george&quot;,
554 Wife =&gt; &quot;jane&quot;,
555 Son =&gt; &quot;elroy&quot;,
558 Lead =&gt; &quot;homer&quot;,
559 Wife =&gt; &quot;marge&quot;,
560 Son =&gt; &quot;bart&quot;,
562 );</pre>
564 </p>
565 <h2><a name="generation_of_an_array_of_hashes">Generation of an ARRAY OF HASHES</a></h2>
566 <pre>
567 # reading from file
568 # format: LEAD=fred FRIEND=barney
569 while ( &lt;&gt; ) {
570 $rec = {};
571 for $field ( split ) {
572 ($key, $value) = split /=/, $field;
573 $rec-&gt;{$key} = $value;
575 push @AoH, $rec;
576 }</pre>
577 <pre>
578 # reading from file
579 # format: LEAD=fred FRIEND=barney
580 # no temp
581 while ( &lt;&gt; ) {
582 push @AoH, { split /[\s+=]/ };
583 }</pre>
584 <pre>
585 # calling a function that returns a key/value pair list, like
586 # &quot;lead&quot;,&quot;fred&quot;,&quot;daughter&quot;,&quot;pebbles&quot;
587 while ( %fields = getnextpairset() ) {
588 push @AoH, { %fields };
589 }</pre>
590 <pre>
591 # likewise, but using no temp vars
592 while (&lt;&gt;) {
593 push @AoH, { parsepairs($_) };
594 }</pre>
595 <pre>
596 # add key/value to an element
597 $AoH[0]{pet} = &quot;dino&quot;;
598 $AoH[2]{pet} = &quot;santa's little helper&quot;;</pre>
600 </p>
601 <h2><a name="access_and_printing_of_an_array_of_hashes">Access and Printing of an ARRAY OF HASHES</a></h2>
602 <pre>
603 # one element
604 $AoH[0]{lead} = &quot;fred&quot;;</pre>
605 <pre>
606 # another element
607 $AoH[1]{lead} =~ s/(\w)/\u$1/;</pre>
608 <pre>
609 # print the whole thing with refs
610 for $href ( @AoH ) {
611 print &quot;{ &quot;;
612 for $role ( keys %$href ) {
613 print &quot;$role=$href-&gt;{$role} &quot;;
615 print &quot;}\n&quot;;
616 }</pre>
617 <pre>
618 # print the whole thing with indices
619 for $i ( 0 .. $#AoH ) {
620 print &quot;$i is { &quot;;
621 for $role ( keys %{ $AoH[$i] } ) {
622 print &quot;$role=$AoH[$i]{$role} &quot;;
624 print &quot;}\n&quot;;
625 }</pre>
626 <pre>
627 # print the whole thing one at a time
628 for $i ( 0 .. $#AoH ) {
629 for $role ( keys %{ $AoH[$i] } ) {
630 print &quot;elt $i $role is $AoH[$i]{$role}\n&quot;;
632 }</pre>
634 </p>
635 <hr />
636 <h1><a name="hashes_of_hashes_x_hass_of_hashes__x_hoh_">HASHES OF HASHES
637 </a></h1>
639 </p>
640 <h2><a name="declaration_of_a_hash_of_hashes">Declaration of a HASH OF HASHES</a></h2>
641 <pre>
642 %HoH = (
643 flintstones =&gt; {
644 lead =&gt; &quot;fred&quot;,
645 pal =&gt; &quot;barney&quot;,
647 jetsons =&gt; {
648 lead =&gt; &quot;george&quot;,
649 wife =&gt; &quot;jane&quot;,
650 &quot;his boy&quot; =&gt; &quot;elroy&quot;,
652 simpsons =&gt; {
653 lead =&gt; &quot;homer&quot;,
654 wife =&gt; &quot;marge&quot;,
655 kid =&gt; &quot;bart&quot;,
657 );</pre>
659 </p>
660 <h2><a name="generation_of_a_hash_of_hashes">Generation of a HASH OF HASHES</a></h2>
661 <pre>
662 # reading from file
663 # flintstones: lead=fred pal=barney wife=wilma pet=dino
664 while ( &lt;&gt; ) {
665 next unless s/^(.*?):\s*//;
666 $who = $1;
667 for $field ( split ) {
668 ($key, $value) = split /=/, $field;
669 $HoH{$who}{$key} = $value;
670 }</pre>
671 <pre>
672 # reading from file; more temps
673 while ( &lt;&gt; ) {
674 next unless s/^(.*?):\s*//;
675 $who = $1;
676 $rec = {};
677 $HoH{$who} = $rec;
678 for $field ( split ) {
679 ($key, $value) = split /=/, $field;
680 $rec-&gt;{$key} = $value;
682 }</pre>
683 <pre>
684 # calling a function that returns a key,value hash
685 for $group ( &quot;simpsons&quot;, &quot;jetsons&quot;, &quot;flintstones&quot; ) {
686 $HoH{$group} = { get_family($group) };
687 }</pre>
688 <pre>
689 # likewise, but using temps
690 for $group ( &quot;simpsons&quot;, &quot;jetsons&quot;, &quot;flintstones&quot; ) {
691 %members = get_family($group);
692 $HoH{$group} = { %members };
693 }</pre>
694 <pre>
695 # append new members to an existing family
696 %new_folks = (
697 wife =&gt; &quot;wilma&quot;,
698 pet =&gt; &quot;dino&quot;,
699 );</pre>
700 <pre>
701 for $what (keys %new_folks) {
702 $HoH{flintstones}{$what} = $new_folks{$what};
703 }</pre>
705 </p>
706 <h2><a name="access_and_printing_of_a_hash_of_hashes">Access and Printing of a HASH OF HASHES</a></h2>
707 <pre>
708 # one element
709 $HoH{flintstones}{wife} = &quot;wilma&quot;;</pre>
710 <pre>
711 # another element
712 $HoH{simpsons}{lead} =~ s/(\w)/\u$1/;</pre>
713 <pre>
714 # print the whole thing
715 foreach $family ( keys %HoH ) {
716 print &quot;$family: { &quot;;
717 for $role ( keys %{ $HoH{$family} } ) {
718 print &quot;$role=$HoH{$family}{$role} &quot;;
720 print &quot;}\n&quot;;
721 }</pre>
722 <pre>
723 # print the whole thing somewhat sorted
724 foreach $family ( sort keys %HoH ) {
725 print &quot;$family: { &quot;;
726 for $role ( sort keys %{ $HoH{$family} } ) {
727 print &quot;$role=$HoH{$family}{$role} &quot;;
729 print &quot;}\n&quot;;
730 }</pre>
731 <pre>
732 # print the whole thing sorted by number of members
733 foreach $family ( sort { keys %{$HoH{$b}} &lt;=&gt; keys %{$HoH{$a}} } keys %HoH ) {
734 print &quot;$family: { &quot;;
735 for $role ( sort keys %{ $HoH{$family} } ) {
736 print &quot;$role=$HoH{$family}{$role} &quot;;
738 print &quot;}\n&quot;;
739 }</pre>
740 <pre>
741 # establish a sort order (rank) for each role
742 $i = 0;
743 for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }</pre>
744 <pre>
745 # now print the whole thing sorted by number of members
746 foreach $family ( sort { keys %{ $HoH{$b} } &lt;=&gt; keys %{ $HoH{$a} } } keys %HoH ) {
747 print &quot;$family: { &quot;;
748 # and print these according to rank order
749 for $role ( sort { $rank{$a} &lt;=&gt; $rank{$b} } keys %{ $HoH{$family} } ) {
750 print &quot;$role=$HoH{$family}{$role} &quot;;
752 print &quot;}\n&quot;;
753 }</pre>
755 </p>
756 <hr />
757 <h1><a name="more_elaborate_records_x_record__x_structure__x_struct_">MORE ELABORATE RECORDS
758 </a></h1>
760 </p>
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>
764 <pre>
765 $rec = {
766 TEXT =&gt; $string,
767 SEQUENCE =&gt; [ @old_values ],
768 LOOKUP =&gt; { %some_table },
769 THATCODE =&gt; \&amp;some_function,
770 THISCODE =&gt; sub { $_[0] ** $_[1] },
771 HANDLE =&gt; \*STDOUT,
772 };</pre>
773 <pre>
774 print $rec-&gt;{TEXT};</pre>
775 <pre>
776 print $rec-&gt;{SEQUENCE}[0];
777 $last = pop @ { $rec-&gt;{SEQUENCE} };</pre>
778 <pre>
779 print $rec-&gt;{LOOKUP}{&quot;key&quot;};
780 ($first_k, $first_v) = each %{ $rec-&gt;{LOOKUP} };</pre>
781 <pre>
782 $answer = $rec-&gt;{THATCODE}-&gt;($arg);
783 $answer = $rec-&gt;{THISCODE}-&gt;($arg1, $arg2);</pre>
784 <pre>
785 # careful of extra block braces on fh ref
786 print { $rec-&gt;{HANDLE} } &quot;a string\n&quot;;</pre>
787 <pre>
788 use FileHandle;
789 $rec-&gt;{HANDLE}-&gt;autoflush(1);
790 $rec-&gt;{HANDLE}-&gt;print(&quot; a string\n&quot;);</pre>
792 </p>
793 <h2><a name="declaration_of_a_hash_of_complex_records">Declaration of a HASH OF COMPLEX RECORDS</a></h2>
794 <pre>
795 %TV = (
796 flintstones =&gt; {
797 series =&gt; &quot;flintstones&quot;,
798 nights =&gt; [ qw(monday thursday friday) ],
799 members =&gt; [
800 { name =&gt; &quot;fred&quot;, role =&gt; &quot;lead&quot;, age =&gt; 36, },
801 { name =&gt; &quot;wilma&quot;, role =&gt; &quot;wife&quot;, age =&gt; 31, },
802 { name =&gt; &quot;pebbles&quot;, role =&gt; &quot;kid&quot;, age =&gt; 4, },
804 },</pre>
805 <pre>
806 jetsons =&gt; {
807 series =&gt; &quot;jetsons&quot;,
808 nights =&gt; [ qw(wednesday saturday) ],
809 members =&gt; [
810 { name =&gt; &quot;george&quot;, role =&gt; &quot;lead&quot;, age =&gt; 41, },
811 { name =&gt; &quot;jane&quot;, role =&gt; &quot;wife&quot;, age =&gt; 39, },
812 { name =&gt; &quot;elroy&quot;, role =&gt; &quot;kid&quot;, age =&gt; 9, },
814 },</pre>
815 <pre>
816 simpsons =&gt; {
817 series =&gt; &quot;simpsons&quot;,
818 nights =&gt; [ qw(monday) ],
819 members =&gt; [
820 { name =&gt; &quot;homer&quot;, role =&gt; &quot;lead&quot;, age =&gt; 34, },
821 { name =&gt; &quot;marge&quot;, role =&gt; &quot;wife&quot;, age =&gt; 37, },
822 { name =&gt; &quot;bart&quot;, role =&gt; &quot;kid&quot;, age =&gt; 11, },
825 );</pre>
827 </p>
828 <h2><a name="generation_of_a_hash_of_complex_records">Generation of a HASH OF COMPLEX RECORDS</a></h2>
829 <pre>
830 # reading from file
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>
835 <pre>
836 # here's a piece by piece build up
837 $rec = {};
838 $rec-&gt;{series} = &quot;flintstones&quot;;
839 $rec-&gt;{nights} = [ find_days() ];</pre>
840 <pre>
841 @members = ();
842 # assume this file in field=value syntax
843 while (&lt;&gt;) {
844 %fields = split /[\s=]+/;
845 push @members, { %fields };
847 $rec-&gt;{members} = [ @members ];</pre>
848 <pre>
849 # now remember the whole thing
850 $TV{ $rec-&gt;{series} } = $rec;</pre>
851 <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
862 @kids = ();
863 for $person ( @{ $rec-&gt;{members} } ) {
864 if ($person-&gt;{role} =~ /kid|son|daughter/) {
865 push @kids, $person;
868 # REMEMBER: $rec and $TV{$family} point to same data!!
869 $rec-&gt;{kids} = [ @kids ];
870 }</pre>
871 <pre>
872 # you copied the array, but the array itself contains pointers
873 # to uncopied objects. this means that if you make bart get
874 # older via</pre>
875 <pre>
876 $TV{simpsons}{kids}[0]{age}++;</pre>
877 <pre>
878 # then this would also change in
879 print $TV{simpsons}{members}[2]{age};</pre>
880 <pre>
881 # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2]
882 # both point to the same underlying anonymous hash table</pre>
883 <pre>
884 # print the whole thing
885 foreach $family ( keys %TV ) {
886 print &quot;the $family&quot;;
887 print &quot; is on during @{ $TV{$family}{nights} }\n&quot;;
888 print &quot;its members are:\n&quot;;
889 for $who ( @{ $TV{$family}{members} } ) {
890 print &quot; $who-&gt;{name} ($who-&gt;{role}), age $who-&gt;{age}\n&quot;;
892 print &quot;it turns out that $TV{$family}{lead} has &quot;;
893 print scalar ( @{ $TV{$family}{kids} } ), &quot; kids named &quot;;
894 print join (&quot;, &quot;, map { $_-&gt;{name} } @{ $TV{$family}{kids} } );
895 print &quot;\n&quot;;
896 }</pre>
898 </p>
899 <hr />
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>
909 </p>
910 <hr />
911 <h1><a name="see_also">SEE ALSO</a></h1>
912 <p>perlref(1), perllol(1), perldata(1), <code>perlobj(1)</code></p>
914 </p>
915 <hr />
916 <h1><a name="author">AUTHOR</a></h1>
917 <p>Tom Christiansen &lt;<em><a href="mailto:tchrist@perl.com">tchrist@perl.com</a></em>&gt;</p>
918 <p>Last update:
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">&nbsp;perldsc - Perl Data Structures Cookbook</span></strong></big>
923 </td></tr>
924 </table>
926 </body>
928 </html>