1PERLLOL(1) Perl Programmers Reference Guide PERLLOL(1)
2
3
4
6 perllol - Manipulating Arrays of Arrays in Perl
7
9 Declaration and Access of Arrays of Arrays
10 The simplest two-level data structure to build in Perl is an array of
11 arrays, sometimes casually called a list of lists. It's reasonably
12 easy to understand, and almost everything that applies here will also
13 be applicable later on with the fancier data structures.
14
15 An array of an array is just a regular old array @AoA that you can get
16 at with two subscripts, like $AoA[3][2]. Here's a declaration of the
17 array:
18
19 use 5.010; # so we can use say()
20
21 # assign to our array, an array of array references
22 @AoA = (
23 [ "fred", "barney", "pebbles", "bambam", "dino", ],
24 [ "george", "jane", "elroy", "judy", ],
25 [ "homer", "bart", "marge", "maggie", ],
26 );
27 say $AoA[2][1];
28 bart
29
30 Now you should be very careful that the outer bracket type is a round
31 one, that is, a parenthesis. That's because you're assigning to an
32 @array, so you need parentheses. If you wanted there not to be an
33 @AoA, but rather just a reference to it, you could do something more
34 like this:
35
36 # assign a reference to array of array references
37 $ref_to_AoA = [
38 [ "fred", "barney", "pebbles", "bambam", "dino", ],
39 [ "george", "jane", "elroy", "judy", ],
40 [ "homer", "bart", "marge", "maggie", ],
41 ];
42 say $ref_to_AoA->[2][1];
43 bart
44
45 Notice that the outer bracket type has changed, and so our access
46 syntax has also changed. That's because unlike C, in perl you can't
47 freely interchange arrays and references thereto. $ref_to_AoA is a
48 reference to an array, whereas @AoA is an array proper. Likewise,
49 $AoA[2] is not an array, but an array ref. So how come you can write
50 these:
51
52 $AoA[2][2]
53 $ref_to_AoA->[2][2]
54
55 instead of having to write these:
56
57 $AoA[2]->[2]
58 $ref_to_AoA->[2]->[2]
59
60 Well, that's because the rule is that on adjacent brackets only
61 (whether square or curly), you are free to omit the pointer
62 dereferencing arrow. But you cannot do so for the very first one if
63 it's a scalar containing a reference, which means that $ref_to_AoA
64 always needs it.
65
66 Growing Your Own
67 That's all well and good for declaration of a fixed data structure, but
68 what if you wanted to add new elements on the fly, or build it up
69 entirely from scratch?
70
71 First, let's look at reading it in from a file. This is something like
72 adding a row at a time. We'll assume that there's a flat file in which
73 each line is a row and each word an element. If you're trying to
74 develop an @AoA array containing all these, here's the right way to do
75 that:
76
77 while (<>) {
78 @tmp = split;
79 push @AoA, [ @tmp ];
80 }
81
82 You might also have loaded that from a function:
83
84 for $i ( 1 .. 10 ) {
85 $AoA[$i] = [ somefunc($i) ];
86 }
87
88 Or you might have had a temporary variable sitting around with the
89 array in it.
90
91 for $i ( 1 .. 10 ) {
92 @tmp = somefunc($i);
93 $AoA[$i] = [ @tmp ];
94 }
95
96 It's important you make sure to use the "[ ]" array reference
97 constructor. That's because this wouldn't work:
98
99 $AoA[$i] = @tmp; # WRONG!
100
101 The reason that doesn't do what you want is because assigning a named
102 array like that to a scalar is taking an array in scalar context, which
103 means just counts the number of elements in @tmp.
104
105 If you are running under "use strict" (and if you aren't, why in the
106 world aren't you?), you'll have to add some declarations to make it
107 happy:
108
109 use strict;
110 my(@AoA, @tmp);
111 while (<>) {
112 @tmp = split;
113 push @AoA, [ @tmp ];
114 }
115
116 Of course, you don't need the temporary array to have a name at all:
117
118 while (<>) {
119 push @AoA, [ split ];
120 }
121
122 You also don't have to use push(). You could just make a direct
123 assignment if you knew where you wanted to put it:
124
125 my (@AoA, $i, $line);
126 for $i ( 0 .. 10 ) {
127 $line = <>;
128 $AoA[$i] = [ split " ", $line ];
129 }
130
131 or even just
132
133 my (@AoA, $i);
134 for $i ( 0 .. 10 ) {
135 $AoA[$i] = [ split " ", <> ];
136 }
137
138 You should in general be leery of using functions that could
139 potentially return lists in scalar context without explicitly stating
140 such. This would be clearer to the casual reader:
141
142 my (@AoA, $i);
143 for $i ( 0 .. 10 ) {
144 $AoA[$i] = [ split " ", scalar(<>) ];
145 }
146
147 If you wanted to have a $ref_to_AoA variable as a reference to an
148 array, you'd have to do something like this:
149
150 while (<>) {
151 push @$ref_to_AoA, [ split ];
152 }
153
154 Now you can add new rows. What about adding new columns? If you're
155 dealing with just matrices, it's often easiest to use simple
156 assignment:
157
158 for $x (1 .. 10) {
159 for $y (1 .. 10) {
160 $AoA[$x][$y] = func($x, $y);
161 }
162 }
163
164 for $x ( 3, 7, 9 ) {
165 $AoA[$x][20] += func2($x);
166 }
167
168 It doesn't matter whether those elements are already there or not:
169 it'll gladly create them for you, setting intervening elements to
170 "undef" as need be.
171
172 If you wanted just to append to a row, you'd have to do something a bit
173 funnier looking:
174
175 # add new columns to an existing row
176 push @{ $AoA[0] }, "wilma", "betty"; # explicit deref
177
178 Prior to Perl 5.14, this wouldn't even compile:
179
180 push $AoA[0], "wilma", "betty"; # implicit deref
181
182 How come? Because once upon a time, the argument to push() had to be a
183 real array, not just a reference to one. That's no longer true. In
184 fact, the line marked "implicit deref" above works just fine--in this
185 instance--to do what the one that says explicit deref did.
186
187 The reason I said "in this instance" is because that only works because
188 $AoA[0] already held an array reference. If you try that on an
189 undefined variable, you'll take an exception. That's because the
190 implicit derefererence will never autovivify an undefined variable the
191 way "@{ }" always will:
192
193 my $aref = undef;
194 push $aref, qw(some more values); # WRONG!
195 push @$aref, qw(a few more); # ok
196
197 If you want to take advantage of this new implicit dereferencing
198 behavior, go right ahead: it makes code easier on the eye and wrist.
199 Just understand that older releases will choke on it during
200 compilation. Whenever you make use of something that works only in
201 some given release of Perl and later, but not earlier, you should place
202 a prominent
203
204 use v5.14; # needed for implicit deref of array refs by array ops
205
206 directive at the top of the file that needs it. That way when somebody
207 tries to run the new code under an old perl, rather than getting an
208 error like
209
210 Type of arg 1 to push must be array (not array element) at /tmp/a line 8, near ""betty";"
211 Execution of /tmp/a aborted due to compilation errors.
212
213 they'll be politely informed that
214
215 Perl v5.14.0 required--this is only v5.12.3, stopped at /tmp/a line 1.
216 BEGIN failed--compilation aborted at /tmp/a line 1.
217
218 Access and Printing
219 Now it's time to print your data structure out. How are you going to
220 do that? Well, if you want only one of the elements, it's trivial:
221
222 print $AoA[0][0];
223
224 If you want to print the whole thing, though, you can't say
225
226 print @AoA; # WRONG
227
228 because you'll get just references listed, and perl will never
229 automatically dereference things for you. Instead, you have to roll
230 yourself a loop or two. This prints the whole structure, using the
231 shell-style for() construct to loop across the outer set of subscripts.
232
233 for $aref ( @AoA ) {
234 say "\t [ @$aref ],";
235 }
236
237 If you wanted to keep track of subscripts, you might do this:
238
239 for $i ( 0 .. $#AoA ) {
240 say "\t elt $i is [ @{$AoA[$i]} ],";
241 }
242
243 or maybe even this. Notice the inner loop.
244
245 for $i ( 0 .. $#AoA ) {
246 for $j ( 0 .. $#{$AoA[$i]} ) {
247 say "elt $i $j is $AoA[$i][$j]";
248 }
249 }
250
251 As you can see, it's getting a bit complicated. That's why sometimes
252 is easier to take a temporary on your way through:
253
254 for $i ( 0 .. $#AoA ) {
255 $aref = $AoA[$i];
256 for $j ( 0 .. $#{$aref} ) {
257 say "elt $i $j is $AoA[$i][$j]";
258 }
259 }
260
261 Hmm... that's still a bit ugly. How about this:
262
263 for $i ( 0 .. $#AoA ) {
264 $aref = $AoA[$i];
265 $n = @$aref - 1;
266 for $j ( 0 .. $n ) {
267 say "elt $i $j is $AoA[$i][$j]";
268 }
269 }
270
271 When you get tired of writing a custom print for your data structures,
272 you might look at the standard Dumpvalue or Data::Dumper modules. The
273 former is what the Perl debugger uses, while the latter generates
274 parsable Perl code. For example:
275
276 use v5.14; # using the + prototype, new to v5.14
277
278 sub show(+) {
279 require Dumpvalue;
280 state $prettily = new Dumpvalue::
281 tick => q("),
282 compactDump => 1, # comment these two lines out
283 veryCompact => 1, # if you want a bigger dump
284 ;
285 dumpValue $prettily @_;
286 }
287
288 # Assign a list of array references to an array.
289 my @AoA = (
290 [ "fred", "barney" ],
291 [ "george", "jane", "elroy" ],
292 [ "homer", "marge", "bart" ],
293 );
294 push $AoA[0], "wilma", "betty";
295 show @AoA;
296
297 will print out:
298
299 0 0..3 "fred" "barney" "wilma" "betty"
300 1 0..2 "george" "jane" "elroy"
301 2 0..2 "homer" "marge" "bart"
302
303 Whereas if you comment out the two lines I said you might wish to, then
304 it shows it to you this way instead:
305
306 0 ARRAY(0x8031d0)
307 0 "fred"
308 1 "barney"
309 2 "wilma"
310 3 "betty"
311 1 ARRAY(0x803d40)
312 0 "george"
313 1 "jane"
314 2 "elroy"
315 2 ARRAY(0x803e10)
316 0 "homer"
317 1 "marge"
318 2 "bart"
319
320 Slices
321 If you want to get at a slice (part of a row) in a multidimensional
322 array, you're going to have to do some fancy subscripting. That's
323 because while we have a nice synonym for single elements via the
324 pointer arrow for dereferencing, no such convenience exists for slices.
325
326 Here's how to do one operation using a loop. We'll assume an @AoA
327 variable as before.
328
329 @part = ();
330 $x = 4;
331 for ($y = 7; $y < 13; $y++) {
332 push @part, $AoA[$x][$y];
333 }
334
335 That same loop could be replaced with a slice operation:
336
337 @part = @{$AoA[4]}[7..12];
338
339 or spaced out a bit:
340
341 @part = @{ $AoA[4] } [ 7..12 ];
342
343 But as you might well imagine, this can get pretty rough on the reader.
344
345 Ah, but what if you wanted a two-dimensional slice, such as having $x
346 run from 4..8 and $y run from 7 to 12? Hmm... here's the simple way:
347
348 @newAoA = ();
349 for ($startx = $x = 4; $x <= 8; $x++) {
350 for ($starty = $y = 7; $y <= 12; $y++) {
351 $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
352 }
353 }
354
355 We can reduce some of the looping through slices
356
357 for ($x = 4; $x <= 8; $x++) {
358 push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
359 }
360
361 If you were into Schwartzian Transforms, you would probably have
362 selected map for that
363
364 @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;
365
366 Although if your manager accused you of seeking job security (or rapid
367 insecurity) through inscrutable code, it would be hard to argue. :-) If
368 I were you, I'd put that in a function:
369
370 @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 );
371 sub splice_2D {
372 my $lrr = shift; # ref to array of array refs!
373 my ($x_lo, $x_hi,
374 $y_lo, $y_hi) = @_;
375
376 return map {
377 [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ]
378 } $x_lo .. $x_hi;
379 }
380
382 perldata, perlref, perldsc
383
385 Tom Christiansen <tchrist@perl.com>
386
387 Last update: Tue Apr 26 18:30:55 MDT 2011
388
389
390
391perl v5.16.3 2013-03-04 PERLLOL(1)