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