Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
8.3 KiB

  1. =head1 NAME
  2. perllol - Manipulating Arrays of Arrays in Perl
  3. =head1 DESCRIPTION
  4. =head2 Declaration and Access of Arrays of Arrays
  5. The simplest thing to build an array of arrays (sometimes imprecisely
  6. called a list of lists). It's reasonably easy to understand, and
  7. almost everything that applies here will also be applicable later
  8. on with the fancier data structures.
  9. An array of an array is just a regular old array @AoA that you can
  10. get at with two subscripts, like C<$AoA[3][2]>. Here's a declaration
  11. of the array:
  12. # assign to our array, an array of array references
  13. @AoA = (
  14. [ "fred", "barney" ],
  15. [ "george", "jane", "elroy" ],
  16. [ "homer", "marge", "bart" ],
  17. );
  18. print $AoA[2][2];
  19. bart
  20. Now you should be very careful that the outer bracket type
  21. is a round one, that is, a parenthesis. That's because you're assigning to
  22. an @array, so you need parentheses. If you wanted there I<not> to be an @AoA,
  23. but rather just a reference to it, you could do something more like this:
  24. # assign a reference to array of array references
  25. $ref_to_AoA = [
  26. [ "fred", "barney", "pebbles", "bambam", "dino", ],
  27. [ "homer", "bart", "marge", "maggie", ],
  28. [ "george", "jane", "elroy", "judy", ],
  29. ];
  30. print $ref_to_AoA->[2][2];
  31. Notice that the outer bracket type has changed, and so our access syntax
  32. has also changed. That's because unlike C, in perl you can't freely
  33. interchange arrays and references thereto. $ref_to_AoA is a reference to an
  34. array, whereas @AoA is an array proper. Likewise, C<$AoA[2]> is not an
  35. array, but an array ref. So how come you can write these:
  36. $AoA[2][2]
  37. $ref_to_AoA->[2][2]
  38. instead of having to write these:
  39. $AoA[2]->[2]
  40. $ref_to_AoA->[2]->[2]
  41. Well, that's because the rule is that on adjacent brackets only (whether
  42. square or curly), you are free to omit the pointer dereferencing arrow.
  43. But you cannot do so for the very first one if it's a scalar containing
  44. a reference, which means that $ref_to_AoA always needs it.
  45. =head2 Growing Your Own
  46. That's all well and good for declaration of a fixed data structure,
  47. but what if you wanted to add new elements on the fly, or build
  48. it up entirely from scratch?
  49. First, let's look at reading it in from a file. This is something like
  50. adding a row at a time. We'll assume that there's a flat file in which
  51. each line is a row and each word an element. If you're trying to develop an
  52. @AoA array containing all these, here's the right way to do that:
  53. while (<>) {
  54. @tmp = split;
  55. push @AoA, [ @tmp ];
  56. }
  57. You might also have loaded that from a function:
  58. for $i ( 1 .. 10 ) {
  59. $AoA[$i] = [ somefunc($i) ];
  60. }
  61. Or you might have had a temporary variable sitting around with the
  62. array in it.
  63. for $i ( 1 .. 10 ) {
  64. @tmp = somefunc($i);
  65. $AoA[$i] = [ @tmp ];
  66. }
  67. It's very important that you make sure to use the C<[]> array reference
  68. constructor. That's because this will be very wrong:
  69. $AoA[$i] = @tmp;
  70. You see, assigning a named array like that to a scalar just counts the
  71. number of elements in @tmp, which probably isn't what you want.
  72. If you are running under C<use strict>, you'll have to add some
  73. declarations to make it happy:
  74. use strict;
  75. my(@AoA, @tmp);
  76. while (<>) {
  77. @tmp = split;
  78. push @AoA, [ @tmp ];
  79. }
  80. Of course, you don't need the temporary array to have a name at all:
  81. while (<>) {
  82. push @AoA, [ split ];
  83. }
  84. You also don't have to use push(). You could just make a direct assignment
  85. if you knew where you wanted to put it:
  86. my (@AoA, $i, $line);
  87. for $i ( 0 .. 10 ) {
  88. $line = <>;
  89. $AoA[$i] = [ split ' ', $line ];
  90. }
  91. or even just
  92. my (@AoA, $i);
  93. for $i ( 0 .. 10 ) {
  94. $AoA[$i] = [ split ' ', <> ];
  95. }
  96. You should in general be leery of using functions that could
  97. potentially return lists in scalar context without explicitly stating
  98. such. This would be clearer to the casual reader:
  99. my (@AoA, $i);
  100. for $i ( 0 .. 10 ) {
  101. $AoA[$i] = [ split ' ', scalar(<>) ];
  102. }
  103. If you wanted to have a $ref_to_AoA variable as a reference to an array,
  104. you'd have to do something like this:
  105. while (<>) {
  106. push @$ref_to_AoA, [ split ];
  107. }
  108. Now you can add new rows. What about adding new columns? If you're
  109. dealing with just matrices, it's often easiest to use simple assignment:
  110. for $x (1 .. 10) {
  111. for $y (1 .. 10) {
  112. $AoA[$x][$y] = func($x, $y);
  113. }
  114. }
  115. for $x ( 3, 7, 9 ) {
  116. $AoA[$x][20] += func2($x);
  117. }
  118. It doesn't matter whether those elements are already
  119. there or not: it'll gladly create them for you, setting
  120. intervening elements to C<undef> as need be.
  121. If you wanted just to append to a row, you'd have
  122. to do something a bit funnier looking:
  123. # add new columns to an existing row
  124. push @{ $AoA[0] }, "wilma", "betty";
  125. Notice that I I<couldn't> say just:
  126. push $AoA[0], "wilma", "betty"; # WRONG!
  127. In fact, that wouldn't even compile. How come? Because the argument
  128. to push() must be a real array, not just a reference to such.
  129. =head2 Access and Printing
  130. Now it's time to print your data structure out. How
  131. are you going to do that? Well, if you want only one
  132. of the elements, it's trivial:
  133. print $AoA[0][0];
  134. If you want to print the whole thing, though, you can't
  135. say
  136. print @AoA; # WRONG
  137. because you'll get just references listed, and perl will never
  138. automatically dereference things for you. Instead, you have to
  139. roll yourself a loop or two. This prints the whole structure,
  140. using the shell-style for() construct to loop across the outer
  141. set of subscripts.
  142. for $aref ( @AoA ) {
  143. print "\t [ @$aref ],\n";
  144. }
  145. If you wanted to keep track of subscripts, you might do this:
  146. for $i ( 0 .. $#AoA ) {
  147. print "\t elt $i is [ @{$AoA[$i]} ],\n";
  148. }
  149. or maybe even this. Notice the inner loop.
  150. for $i ( 0 .. $#AoA ) {
  151. for $j ( 0 .. $#{$AoA[$i]} ) {
  152. print "elt $i $j is $AoA[$i][$j]\n";
  153. }
  154. }
  155. As you can see, it's getting a bit complicated. That's why
  156. sometimes is easier to take a temporary on your way through:
  157. for $i ( 0 .. $#AoA ) {
  158. $aref = $AoA[$i];
  159. for $j ( 0 .. $#{$aref} ) {
  160. print "elt $i $j is $AoA[$i][$j]\n";
  161. }
  162. }
  163. Hmm... that's still a bit ugly. How about this:
  164. for $i ( 0 .. $#AoA ) {
  165. $aref = $AoA[$i];
  166. $n = @$aref - 1;
  167. for $j ( 0 .. $n ) {
  168. print "elt $i $j is $AoA[$i][$j]\n";
  169. }
  170. }
  171. =head2 Slices
  172. If you want to get at a slice (part of a row) in a multidimensional
  173. array, you're going to have to do some fancy subscripting. That's
  174. because while we have a nice synonym for single elements via the
  175. pointer arrow for dereferencing, no such convenience exists for slices.
  176. (Remember, of course, that you can always write a loop to do a slice
  177. operation.)
  178. Here's how to do one operation using a loop. We'll assume an @AoA
  179. variable as before.
  180. @part = ();
  181. $x = 4;
  182. for ($y = 7; $y < 13; $y++) {
  183. push @part, $AoA[$x][$y];
  184. }
  185. That same loop could be replaced with a slice operation:
  186. @part = @{ $AoA[4] } [ 7..12 ];
  187. but as you might well imagine, this is pretty rough on the reader.
  188. Ah, but what if you wanted a I<two-dimensional slice>, such as having
  189. $x run from 4..8 and $y run from 7 to 12? Hmm... here's the simple way:
  190. @newAoA = ();
  191. for ($startx = $x = 4; $x <= 8; $x++) {
  192. for ($starty = $y = 7; $y <= 12; $y++) {
  193. $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
  194. }
  195. }
  196. We can reduce some of the looping through slices
  197. for ($x = 4; $x <= 8; $x++) {
  198. push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
  199. }
  200. If you were into Schwartzian Transforms, you would probably
  201. have selected map for that
  202. @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;
  203. Although if your manager accused of seeking job security (or rapid
  204. insecurity) through inscrutable code, it would be hard to argue. :-)
  205. If I were you, I'd put that in a function:
  206. @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 );
  207. sub splice_2D {
  208. my $lrr = shift; # ref to array of array refs!
  209. my ($x_lo, $x_hi,
  210. $y_lo, $y_hi) = @_;
  211. return map {
  212. [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ]
  213. } $x_lo .. $x_hi;
  214. }
  215. =head1 SEE ALSO
  216. perldata(1), perlref(1), perldsc(1)
  217. =head1 AUTHOR
  218. Tom Christiansen <F<[email protected]>>
  219. Last update: Thu Jun 4 16:16:23 MDT 1998