1PERLLOL(1)             Perl Programmers Reference Guide             PERLLOL(1)
2
3
4

NAME

6       perllol - Manipulating Arrays of Arrays in Perl
7

DESCRIPTION

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   Access and Printing
179       Now it's time to print your data structure out.  How are you going to
180       do that?  Well, if you want only one of the elements, it's trivial:
181
182           print $AoA[0][0];
183
184       If you want to print the whole thing, though, you can't say
185
186           print @AoA;         # WRONG
187
188       because you'll get just references listed, and perl will never
189       automatically dereference things for you.  Instead, you have to roll
190       yourself a loop or two.  This prints the whole structure, using the
191       shell-style for() construct to loop across the outer set of subscripts.
192
193           for $aref ( @AoA ) {
194               say "\t [ @$aref ],";
195           }
196
197       If you wanted to keep track of subscripts, you might do this:
198
199           for $i ( 0 .. $#AoA ) {
200               say "\t elt $i is [ @{$AoA[$i]} ],";
201           }
202
203       or maybe even this.  Notice the inner loop.
204
205           for $i ( 0 .. $#AoA ) {
206               for $j ( 0 .. $#{$AoA[$i]} ) {
207                   say "elt $i $j is $AoA[$i][$j]";
208               }
209           }
210
211       As you can see, it's getting a bit complicated.  That's why sometimes
212       is easier to take a temporary on your way through:
213
214           for $i ( 0 .. $#AoA ) {
215               $aref = $AoA[$i];
216               for $j ( 0 .. $#{$aref} ) {
217                   say "elt $i $j is $AoA[$i][$j]";
218               }
219           }
220
221       Hmm... that's still a bit ugly.  How about this:
222
223           for $i ( 0 .. $#AoA ) {
224               $aref = $AoA[$i];
225               $n = @$aref - 1;
226               for $j ( 0 .. $n ) {
227                   say "elt $i $j is $AoA[$i][$j]";
228               }
229           }
230
231       When you get tired of writing a custom print for your data structures,
232       you might look at the standard Dumpvalue or Data::Dumper modules.  The
233       former is what the Perl debugger uses, while the latter generates
234       parsable Perl code.  For example:
235
236        use v5.14;     # using the + prototype, new to v5.14
237
238        sub show(+) {
239               require Dumpvalue;
240               state $prettily = new Dumpvalue::
241                                   tick        => q("),
242                                   compactDump => 1,  # comment these two lines
243                                                      # out
244                                   veryCompact => 1,  # if you want a bigger
245                                                      # dump
246                               ;
247               dumpValue $prettily @_;
248        }
249
250        # Assign a list of array references to an array.
251        my @AoA = (
252                  [ "fred", "barney" ],
253                  [ "george", "jane", "elroy" ],
254                  [ "homer", "marge", "bart" ],
255        );
256        push $AoA[0]->@*, "wilma", "betty";
257        show @AoA;
258
259       will print out:
260
261           0  0..3  "fred" "barney" "wilma" "betty"
262           1  0..2  "george" "jane" "elroy"
263           2  0..2  "homer" "marge" "bart"
264
265       Whereas if you comment out the two lines I said you might wish to, then
266       it shows it to you this way instead:
267
268           0  ARRAY(0x8031d0)
269              0  "fred"
270              1  "barney"
271              2  "wilma"
272              3  "betty"
273           1  ARRAY(0x803d40)
274              0  "george"
275              1  "jane"
276              2  "elroy"
277           2  ARRAY(0x803e10)
278              0  "homer"
279              1  "marge"
280              2  "bart"
281
282   Slices
283       If you want to get at a slice (part of a row) in a multidimensional
284       array, you're going to have to do some fancy subscripting.  That's
285       because while we have a nice synonym for single elements via the
286       pointer arrow for dereferencing, no such convenience exists for slices.
287
288       Here's how to do one operation using a loop.  We'll assume an @AoA
289       variable as before.
290
291           @part = ();
292           $x = 4;
293           for ($y = 7; $y < 13; $y++) {
294               push @part, $AoA[$x][$y];
295           }
296
297       That same loop could be replaced with a slice operation:
298
299           @part = $AoA[4]->@[ 7..12 ];
300
301       Now, what if you wanted a two-dimensional slice, such as having $x run
302       from 4..8 and $y run from 7 to 12?  Hmm... here's the simple way:
303
304           @newAoA = ();
305           for ($startx = $x = 4; $x <= 8; $x++) {
306               for ($starty = $y = 7; $y <= 12; $y++) {
307                   $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
308               }
309           }
310
311       We can reduce some of the looping through slices
312
313           for ($x = 4; $x <= 8; $x++) {
314               push @newAoA, [ $AoA[$x]->@[ 7..12 ] ];
315           }
316
317       If you were into Schwartzian Transforms, you would probably have
318       selected map for that
319
320           @newAoA = map { [ $AoA[$_]->@[ 7..12 ] ] } 4 .. 8;
321
322       Although if your manager accused you of seeking job security (or rapid
323       insecurity) through inscrutable code, it would be hard to argue. :-) If
324       I were you, I'd put that in a function:
325
326           @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 );
327           sub splice_2D {
328               my $lrr = shift;        # ref to array of array refs!
329               my ($x_lo, $x_hi,
330                   $y_lo, $y_hi) = @_;
331
332               return map {
333                   [ $lrr->[$_]->@[ $y_lo .. $y_hi ] ]
334               } $x_lo .. $x_hi;
335           }
336

SEE ALSO

338       perldata, perlref, perldsc
339

AUTHOR

341       Tom Christiansen <tchrist@perl.com>
342
343       Last update: Tue Apr 26 18:30:55 MDT 2011
344
345
346
347perl v5.34.1                      2022-03-15                        PERLLOL(1)
Impressum