Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perllol.html
blob18358f55b12d9cc1bea088102eb081005210e6bb
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>perllol - Manipulating Arrays of Arrays in Perl</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;perllol - Manipulating Arrays of Arrays in Perl</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 <ul>
26 <li><a href="#declaration_and_access_of_arrays_of_arrays">Declaration and Access of Arrays of Arrays</a></li>
27 <li><a href="#growing_your_own">Growing Your Own</a></li>
28 <li><a href="#access_and_printing">Access and Printing</a></li>
29 <li><a href="#slices">Slices</a></li>
30 </ul>
32 <li><a href="#see_also">SEE ALSO</a></li>
33 <li><a href="#author">AUTHOR</a></li>
34 </ul>
35 <!-- INDEX END -->
37 <hr />
38 <p>
39 </p>
40 <h1><a name="name">NAME</a></h1>
41 <p>perllol - Manipulating Arrays of Arrays in Perl</p>
42 <p>
43 </p>
44 <hr />
45 <h1><a name="description">DESCRIPTION</a></h1>
46 <p>
47 </p>
48 <h2><a name="declaration_and_access_of_arrays_of_arrays">Declaration and Access of Arrays of Arrays</a></h2>
49 <p>The simplest thing to build is an array of arrays (sometimes imprecisely
50 called a list of lists). It's reasonably easy to understand, and
51 almost everything that applies here will also be applicable later
52 on with the fancier data structures.</p>
53 <p>An array of an array is just a regular old array @AoA that you can
54 get at with two subscripts, like <code>$AoA[3][2]</code>. Here's a declaration
55 of the array:</p>
56 <pre>
57 # assign to our array, an array of array references
58 @AoA = (
59 [ &quot;fred&quot;, &quot;barney&quot; ],
60 [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot; ],
61 [ &quot;homer&quot;, &quot;marge&quot;, &quot;bart&quot; ],
62 );</pre>
63 <pre>
64 print $AoA[2][2];
65 bart</pre>
66 <p>Now you should be very careful that the outer bracket type
67 is a round one, that is, a parenthesis. That's because you're assigning to
68 an @array, so you need parentheses. If you wanted there <em>not</em> to be an @AoA,
69 but rather just a reference to it, you could do something more like this:</p>
70 <pre>
71 # assign a reference to array of array references
72 $ref_to_AoA = [
73 [ &quot;fred&quot;, &quot;barney&quot;, &quot;pebbles&quot;, &quot;bambam&quot;, &quot;dino&quot;, ],
74 [ &quot;homer&quot;, &quot;bart&quot;, &quot;marge&quot;, &quot;maggie&quot;, ],
75 [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot;, &quot;judy&quot;, ],
76 ];</pre>
77 <pre>
78 print $ref_to_AoA-&gt;[2][2];</pre>
79 <p>Notice that the outer bracket type has changed, and so our access syntax
80 has also changed. That's because unlike C, in perl you can't freely
81 interchange arrays and references thereto. $ref_to_AoA is a reference to an
82 array, whereas @AoA is an array proper. Likewise, <code>$AoA[2]</code> is not an
83 array, but an array ref. So how come you can write these:</p>
84 <pre>
85 $AoA[2][2]
86 $ref_to_AoA-&gt;[2][2]</pre>
87 <p>instead of having to write these:</p>
88 <pre>
89 $AoA[2]-&gt;[2]
90 $ref_to_AoA-&gt;[2]-&gt;[2]</pre>
91 <p>Well, that's because the rule is that on adjacent brackets only (whether
92 square or curly), you are free to omit the pointer dereferencing arrow.
93 But you cannot do so for the very first one if it's a scalar containing
94 a reference, which means that $ref_to_AoA always needs it.</p>
95 <p>
96 </p>
97 <h2><a name="growing_your_own">Growing Your Own</a></h2>
98 <p>That's all well and good for declaration of a fixed data structure,
99 but what if you wanted to add new elements on the fly, or build
100 it up entirely from scratch?</p>
101 <p>First, let's look at reading it in from a file. This is something like
102 adding a row at a time. We'll assume that there's a flat file in which
103 each line is a row and each word an element. If you're trying to develop an
104 @AoA array containing all these, here's the right way to do that:</p>
105 <pre>
106 while (&lt;&gt;) {
107 @tmp = split;
108 push @AoA, [ @tmp ];
109 }</pre>
110 <p>You might also have loaded that from a function:</p>
111 <pre>
112 for $i ( 1 .. 10 ) {
113 $AoA[$i] = [ somefunc($i) ];
114 }</pre>
115 <p>Or you might have had a temporary variable sitting around with the
116 array in it.</p>
117 <pre>
118 for $i ( 1 .. 10 ) {
119 @tmp = somefunc($i);
120 $AoA[$i] = [ @tmp ];
121 }</pre>
122 <p>It's very important that you make sure to use the <code>[]</code> array reference
123 constructor. That's because this will be very wrong:</p>
124 <pre>
125 $AoA[$i] = @tmp;</pre>
126 <p>You see, assigning a named array like that to a scalar just counts the
127 number of elements in @tmp, which probably isn't what you want.</p>
128 <p>If you are running under <code>use strict</code>, you'll have to add some
129 declarations to make it happy:</p>
130 <pre>
131 use strict;
132 my(@AoA, @tmp);
133 while (&lt;&gt;) {
134 @tmp = split;
135 push @AoA, [ @tmp ];
136 }</pre>
137 <p>Of course, you don't need the temporary array to have a name at all:</p>
138 <pre>
139 while (&lt;&gt;) {
140 push @AoA, [ split ];
141 }</pre>
142 <p>You also don't have to use push(). You could just make a direct assignment
143 if you knew where you wanted to put it:</p>
144 <pre>
145 my (@AoA, $i, $line);
146 for $i ( 0 .. 10 ) {
147 $line = &lt;&gt;;
148 $AoA[$i] = [ split ' ', $line ];
149 }</pre>
150 <p>or even just</p>
151 <pre>
152 my (@AoA, $i);
153 for $i ( 0 .. 10 ) {
154 $AoA[$i] = [ split ' ', &lt;&gt; ];
155 }</pre>
156 <p>You should in general be leery of using functions that could
157 potentially return lists in scalar context without explicitly stating
158 such. This would be clearer to the casual reader:</p>
159 <pre>
160 my (@AoA, $i);
161 for $i ( 0 .. 10 ) {
162 $AoA[$i] = [ split ' ', scalar(&lt;&gt;) ];
163 }</pre>
164 <p>If you wanted to have a $ref_to_AoA variable as a reference to an array,
165 you'd have to do something like this:</p>
166 <pre>
167 while (&lt;&gt;) {
168 push @$ref_to_AoA, [ split ];
169 }</pre>
170 <p>Now you can add new rows. What about adding new columns? If you're
171 dealing with just matrices, it's often easiest to use simple assignment:</p>
172 <pre>
173 for $x (1 .. 10) {
174 for $y (1 .. 10) {
175 $AoA[$x][$y] = func($x, $y);
177 }</pre>
178 <pre>
179 for $x ( 3, 7, 9 ) {
180 $AoA[$x][20] += func2($x);
181 }</pre>
182 <p>It doesn't matter whether those elements are already
183 there or not: it'll gladly create them for you, setting
184 intervening elements to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a> as need be.</p>
185 <p>If you wanted just to append to a row, you'd have
186 to do something a bit funnier looking:</p>
187 <pre>
188 # add new columns to an existing row
189 push @{ $AoA[0] }, &quot;wilma&quot;, &quot;betty&quot;;</pre>
190 <p>Notice that I <em>couldn't</em> say just:</p>
191 <pre>
192 push $AoA[0], &quot;wilma&quot;, &quot;betty&quot;; # WRONG!</pre>
193 <p>In fact, that wouldn't even compile. How come? Because the argument
194 to <code>push()</code> must be a real array, not just a reference to such.</p>
196 </p>
197 <h2><a name="access_and_printing">Access and Printing</a></h2>
198 <p>Now it's time to print your data structure out. How
199 are you going to do that? Well, if you want only one
200 of the elements, it's trivial:</p>
201 <pre>
202 print $AoA[0][0];</pre>
203 <p>If you want to print the whole thing, though, you can't
204 say</p>
205 <pre>
206 print @AoA; # WRONG</pre>
207 <p>because you'll get just references listed, and perl will never
208 automatically dereference things for you. Instead, you have to
209 roll yourself a loop or two. This prints the whole structure,
210 using the shell-style <code>for()</code> construct to loop across the outer
211 set of subscripts.</p>
212 <pre>
213 for $aref ( @AoA ) {
214 print &quot;\t [ @$aref ],\n&quot;;
215 }</pre>
216 <p>If you wanted to keep track of subscripts, you might do this:</p>
217 <pre>
218 for $i ( 0 .. $#AoA ) {
219 print &quot;\t elt $i is [ @{$AoA[$i]} ],\n&quot;;
220 }</pre>
221 <p>or maybe even this. Notice the inner loop.</p>
222 <pre>
223 for $i ( 0 .. $#AoA ) {
224 for $j ( 0 .. $#{$AoA[$i]} ) {
225 print &quot;elt $i $j is $AoA[$i][$j]\n&quot;;
227 }</pre>
228 <p>As you can see, it's getting a bit complicated. That's why
229 sometimes is easier to take a temporary on your way through:</p>
230 <pre>
231 for $i ( 0 .. $#AoA ) {
232 $aref = $AoA[$i];
233 for $j ( 0 .. $#{$aref} ) {
234 print &quot;elt $i $j is $AoA[$i][$j]\n&quot;;
236 }</pre>
237 <p>Hmm... that's still a bit ugly. How about this:</p>
238 <pre>
239 for $i ( 0 .. $#AoA ) {
240 $aref = $AoA[$i];
241 $n = @$aref - 1;
242 for $j ( 0 .. $n ) {
243 print &quot;elt $i $j is $AoA[$i][$j]\n&quot;;
245 }</pre>
247 </p>
248 <h2><a name="slices">Slices</a></h2>
249 <p>If you want to get at a slice (part of a row) in a multidimensional
250 array, you're going to have to do some fancy subscripting. That's
251 because while we have a nice synonym for single elements via the
252 pointer arrow for dereferencing, no such convenience exists for slices.
253 (Remember, of course, that you can always write a loop to do a slice
254 operation.)</p>
255 <p>Here's how to do one operation using a loop. We'll assume an @AoA
256 variable as before.</p>
257 <pre>
258 @part = ();
259 $x = 4;
260 for ($y = 7; $y &lt; 13; $y++) {
261 push @part, $AoA[$x][$y];
262 }</pre>
263 <p>That same loop could be replaced with a slice operation:</p>
264 <pre>
265 @part = @{ $AoA[4] } [ 7..12 ];</pre>
266 <p>but as you might well imagine, this is pretty rough on the reader.</p>
267 <p>Ah, but what if you wanted a <em>two-dimensional slice</em>, such as having
268 $x run from 4..8 and $y run from 7 to 12? Hmm... here's the simple way:</p>
269 <pre>
270 @newAoA = ();
271 for ($startx = $x = 4; $x &lt;= 8; $x++) {
272 for ($starty = $y = 7; $y &lt;= 12; $y++) {
273 $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
275 }</pre>
276 <p>We can reduce some of the looping through slices</p>
277 <pre>
278 for ($x = 4; $x &lt;= 8; $x++) {
279 push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
280 }</pre>
281 <p>If you were into Schwartzian Transforms, you would probably
282 have selected map for that</p>
283 <pre>
284 @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;</pre>
285 <p>Although if your manager accused of seeking job security (or rapid
286 insecurity) through inscrutable code, it would be hard to argue. :-)
287 If I were you, I'd put that in a function:</p>
288 <pre>
289 @newAoA = splice_2D( \@AoA, 4 =&gt; 8, 7 =&gt; 12 );
290 sub splice_2D {
291 my $lrr = shift; # ref to array of array refs!
292 my ($x_lo, $x_hi,
293 $y_lo, $y_hi) = @_;</pre>
294 <pre>
295 return map {
296 [ @{ $lrr-&gt;[$_] } [ $y_lo .. $y_hi ] ]
297 } $x_lo .. $x_hi;
298 }</pre>
300 </p>
301 <hr />
302 <h1><a name="see_also">SEE ALSO</a></h1>
303 <p>perldata(1), perlref(1), <code>perldsc(1)</code></p>
305 </p>
306 <hr />
307 <h1><a name="author">AUTHOR</a></h1>
308 <p>Tom Christiansen &lt;<em><a href="mailto:tchrist@perl.com">tchrist@perl.com</a></em>&gt;</p>
309 <p>Last update: Thu Jun 4 16:16:23 MDT 1998</p>
310 <table border="0" width="100%" cellspacing="0" cellpadding="3">
311 <tr><td class="block" style="background-color: #cccccc" valign="middle">
312 <big><strong><span class="block">&nbsp;perllol - Manipulating Arrays of Arrays in Perl</span></strong></big>
313 </td></tr>
314 </table>
316 </body>
318 </html>