X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperldsc.pod;h=db415343c1fea9310d50a51b9898199665946deb;hb=7360c6b444ea6e19b6583f9530f845bee19c7921;hp=5ab97e179512bed870a82b123ea32b2479652fc0;hpb=19799a22062ef658e4ac543ea06fa9193323512a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perldsc.pod b/pod/perldsc.pod index 5ab97e1..db41534 100644 --- a/pod/perldsc.pod +++ b/pod/perldsc.pod @@ -1,4 +1,5 @@ =head1 NAME +X X X perldsc - Perl Data Structures Cookbook @@ -15,7 +16,7 @@ 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 a array +may now write something like this and all of a sudden, you'd have an array with three dimensions! for $x (1 .. 10) { @@ -32,7 +33,7 @@ elaborate construct than meets the eye! How do you print it out? Why can't you say just C? How do you sort it? How can you pass it to a function or get one of these back -from a function? Is is an object? Can you save it to disk to read +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? @@ -68,15 +69,17 @@ But for now, let's look at general issues common to all these types of data structures. =head1 REFERENCES +X X X X -The most important thing to understand about all data structures in Perl --- including multidimensional arrays--is that even though they might +The most important thing to understand about all data structures in +Perl--including multidimensional arrays--is that even though they might appear otherwise, Perl C<@ARRAY>s and C<%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 I to other arrays or hashes. +X X -You can't use a reference to a array or hash in quite the same way that you +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 @@ -174,6 +177,7 @@ 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 C<[]> or the hash constructor C<{}> instead. Here's the right way to do the preceding broken code fragments: +X<[]> X<{}> for $i (1..10) { @array = somefunc($i); @@ -195,7 +199,7 @@ much harder to read: 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 I of the data. -Something else could be going on in this new case with the C<@{$AoA[$i]}}> +Something else could be going on in this new case with the C<@{$AoA[$i]}> dereference on the left-hand-side of the assignment. It all depends on whether C<$AoA[$i]> had been undefined to start with, or whether it already contained a reference. If you had already populated @AoA with @@ -248,9 +252,11 @@ In summary: =head1 CAVEAT ON PRECEDENCE +X X Speaking of things like C<@{$AoA[$i]}>, the following are actually the same thing: +X<< -> >> $aref->[2][2] # clear $$aref[2][2] # confusing @@ -298,6 +304,10 @@ variable, and it would thereby remind you to write instead: print $aref->[2][2] =head1 DEBUGGING +X X +X X X X +X X +X X 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 @@ -331,8 +341,9 @@ here are short code examples illustrating access of various types of data structures. =head1 ARRAYS OF ARRAYS +X X -=head2 Declaration of a ARRAY OF ARRAYS +=head2 Declaration of an ARRAY OF ARRAYS @AoA = ( [ "fred", "barney" ], @@ -340,7 +351,7 @@ types of data structures. [ "homer", "marge", "bart" ], ); -=head2 Generation of a ARRAY OF ARRAYS +=head2 Generation of an ARRAY OF ARRAYS # reading from file while ( <> ) { @@ -361,7 +372,7 @@ types of data structures. # add to an existing row push @{ $AoA[0] }, "wilma", "betty"; -=head2 Access and Printing of a ARRAY OF ARRAYS +=head2 Access and Printing of an ARRAY OF ARRAYS # one element $AoA[0][0] = "Fred"; @@ -387,6 +398,7 @@ types of data structures. } =head1 HASHES OF ARRAYS +X X =head2 Declaration of a HASH OF ARRAYS @@ -465,8 +477,9 @@ types of data structures. } =head1 ARRAYS OF HASHES +X X -=head2 Declaration of a ARRAY OF HASHES +=head2 Declaration of an ARRAY OF HASHES @AoH = ( { @@ -485,7 +498,7 @@ types of data structures. } ); -=head2 Generation of a ARRAY OF HASHES +=head2 Generation of an ARRAY OF HASHES # reading from file # format: LEAD=fred FRIEND=barney @@ -521,7 +534,7 @@ types of data structures. $AoH[0]{pet} = "dino"; $AoH[2]{pet} = "santa's little helper"; -=head2 Access and Printing of a ARRAY OF HASHES +=head2 Access and Printing of an ARRAY OF HASHES # one element $AoH[0]{lead} = "fred"; @@ -555,6 +568,7 @@ types of data structures. } =head1 HASHES OF HASHES +X X =head2 Declaration of a HASH OF HASHES @@ -673,6 +687,7 @@ types of data structures. =head1 MORE ELABORATE RECORDS +X X X =head2 Declaration of MORE ELABORATE RECORDS