1N/A=head1 NAME
1N/A
1N/Aperllol - Manipulating Arrays of Arrays in Perl
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/A=head2 Declaration and Access of Arrays of Arrays
1N/A
1N/AThe simplest thing to build is an array of arrays (sometimes imprecisely
1N/Acalled a list of lists). It's reasonably easy to understand, and
1N/Aalmost everything that applies here will also be applicable later
1N/Aon with the fancier data structures.
1N/A
1N/AAn array of an array is just a regular old array @AoA that you can
1N/Aget at with two subscripts, like C<$AoA[3][2]>. Here's a declaration
1N/Aof the array:
1N/A
1N/A # assign to our array, an array of array references
1N/A @AoA = (
1N/A [ "fred", "barney" ],
1N/A [ "george", "jane", "elroy" ],
1N/A [ "homer", "marge", "bart" ],
1N/A );
1N/A
1N/A print $AoA[2][2];
1N/A bart
1N/A
1N/ANow you should be very careful that the outer bracket type
1N/Ais a round one, that is, a parenthesis. That's because you're assigning to
1N/Aan @array, so you need parentheses. If you wanted there I<not> to be an @AoA,
1N/Abut rather just a reference to it, you could do something more like this:
1N/A
1N/A # assign a reference to array of array references
1N/A $ref_to_AoA = [
1N/A [ "fred", "barney", "pebbles", "bambam", "dino", ],
1N/A [ "homer", "bart", "marge", "maggie", ],
1N/A [ "george", "jane", "elroy", "judy", ],
1N/A ];
1N/A
1N/A print $ref_to_AoA->[2][2];
1N/A
1N/ANotice that the outer bracket type has changed, and so our access syntax
1N/Ahas also changed. That's because unlike C, in perl you can't freely
1N/Ainterchange arrays and references thereto. $ref_to_AoA is a reference to an
1N/Aarray, whereas @AoA is an array proper. Likewise, C<$AoA[2]> is not an
1N/Aarray, but an array ref. So how come you can write these:
1N/A
1N/A $AoA[2][2]
1N/A $ref_to_AoA->[2][2]
1N/A
1N/Ainstead of having to write these:
1N/A
1N/A $AoA[2]->[2]
1N/A $ref_to_AoA->[2]->[2]
1N/A
1N/AWell, that's because the rule is that on adjacent brackets only (whether
1N/Asquare or curly), you are free to omit the pointer dereferencing arrow.
1N/ABut you cannot do so for the very first one if it's a scalar containing
1N/Aa reference, which means that $ref_to_AoA always needs it.
1N/A
1N/A=head2 Growing Your Own
1N/A
1N/AThat's all well and good for declaration of a fixed data structure,
1N/Abut what if you wanted to add new elements on the fly, or build
1N/Ait up entirely from scratch?
1N/A
1N/AFirst, let's look at reading it in from a file. This is something like
1N/Aadding a row at a time. We'll assume that there's a flat file in which
1N/Aeach line is a row and each word an element. If you're trying to develop an
1N/A@AoA array containing all these, here's the right way to do that:
1N/A
1N/A while (<>) {
1N/A @tmp = split;
1N/A push @AoA, [ @tmp ];
1N/A }
1N/A
1N/AYou might also have loaded that from a function:
1N/A
1N/A for $i ( 1 .. 10 ) {
1N/A $AoA[$i] = [ somefunc($i) ];
1N/A }
1N/A
1N/AOr you might have had a temporary variable sitting around with the
1N/Aarray in it.
1N/A
1N/A for $i ( 1 .. 10 ) {
1N/A @tmp = somefunc($i);
1N/A $AoA[$i] = [ @tmp ];
1N/A }
1N/A
1N/AIt's very important that you make sure to use the C<[]> array reference
1N/Aconstructor. That's because this will be very wrong:
1N/A
1N/A $AoA[$i] = @tmp;
1N/A
1N/AYou see, assigning a named array like that to a scalar just counts the
1N/Anumber of elements in @tmp, which probably isn't what you want.
1N/A
1N/AIf you are running under C<use strict>, you'll have to add some
1N/Adeclarations to make it happy:
1N/A
1N/A use strict;
1N/A my(@AoA, @tmp);
1N/A while (<>) {
1N/A @tmp = split;
1N/A push @AoA, [ @tmp ];
1N/A }
1N/A
1N/AOf course, you don't need the temporary array to have a name at all:
1N/A
1N/A while (<>) {
1N/A push @AoA, [ split ];
1N/A }
1N/A
1N/AYou also don't have to use push(). You could just make a direct assignment
1N/Aif you knew where you wanted to put it:
1N/A
1N/A my (@AoA, $i, $line);
1N/A for $i ( 0 .. 10 ) {
1N/A $line = <>;
1N/A $AoA[$i] = [ split ' ', $line ];
1N/A }
1N/A
1N/Aor even just
1N/A
1N/A my (@AoA, $i);
1N/A for $i ( 0 .. 10 ) {
1N/A $AoA[$i] = [ split ' ', <> ];
1N/A }
1N/A
1N/AYou should in general be leery of using functions that could
1N/Apotentially return lists in scalar context without explicitly stating
1N/Asuch. This would be clearer to the casual reader:
1N/A
1N/A my (@AoA, $i);
1N/A for $i ( 0 .. 10 ) {
1N/A $AoA[$i] = [ split ' ', scalar(<>) ];
1N/A }
1N/A
1N/AIf you wanted to have a $ref_to_AoA variable as a reference to an array,
1N/Ayou'd have to do something like this:
1N/A
1N/A while (<>) {
1N/A push @$ref_to_AoA, [ split ];
1N/A }
1N/A
1N/ANow you can add new rows. What about adding new columns? If you're
1N/Adealing with just matrices, it's often easiest to use simple assignment:
1N/A
1N/A for $x (1 .. 10) {
1N/A for $y (1 .. 10) {
1N/A $AoA[$x][$y] = func($x, $y);
1N/A }
1N/A }
1N/A
1N/A for $x ( 3, 7, 9 ) {
1N/A $AoA[$x][20] += func2($x);
1N/A }
1N/A
1N/AIt doesn't matter whether those elements are already
1N/Athere or not: it'll gladly create them for you, setting
1N/Aintervening elements to C<undef> as need be.
1N/A
1N/AIf you wanted just to append to a row, you'd have
1N/Ato do something a bit funnier looking:
1N/A
1N/A # add new columns to an existing row
1N/A push @{ $AoA[0] }, "wilma", "betty";
1N/A
1N/ANotice that I I<couldn't> say just:
1N/A
1N/A push $AoA[0], "wilma", "betty"; # WRONG!
1N/A
1N/AIn fact, that wouldn't even compile. How come? Because the argument
1N/Ato push() must be a real array, not just a reference to such.
1N/A
1N/A=head2 Access and Printing
1N/A
1N/ANow it's time to print your data structure out. How
1N/Aare you going to do that? Well, if you want only one
1N/Aof the elements, it's trivial:
1N/A
1N/A print $AoA[0][0];
1N/A
1N/AIf you want to print the whole thing, though, you can't
1N/Asay
1N/A
1N/A print @AoA; # WRONG
1N/A
1N/Abecause you'll get just references listed, and perl will never
1N/Aautomatically dereference things for you. Instead, you have to
1N/Aroll yourself a loop or two. This prints the whole structure,
1N/Ausing the shell-style for() construct to loop across the outer
1N/Aset of subscripts.
1N/A
1N/A for $aref ( @AoA ) {
1N/A print "\t [ @$aref ],\n";
1N/A }
1N/A
1N/AIf you wanted to keep track of subscripts, you might do this:
1N/A
1N/A for $i ( 0 .. $#AoA ) {
1N/A print "\t elt $i is [ @{$AoA[$i]} ],\n";
1N/A }
1N/A
1N/Aor maybe even this. Notice the inner loop.
1N/A
1N/A for $i ( 0 .. $#AoA ) {
1N/A for $j ( 0 .. $#{$AoA[$i]} ) {
1N/A print "elt $i $j is $AoA[$i][$j]\n";
1N/A }
1N/A }
1N/A
1N/AAs you can see, it's getting a bit complicated. That's why
1N/Asometimes is easier to take a temporary on your way through:
1N/A
1N/A for $i ( 0 .. $#AoA ) {
1N/A $aref = $AoA[$i];
1N/A for $j ( 0 .. $#{$aref} ) {
1N/A print "elt $i $j is $AoA[$i][$j]\n";
1N/A }
1N/A }
1N/A
1N/AHmm... that's still a bit ugly. How about this:
1N/A
1N/A for $i ( 0 .. $#AoA ) {
1N/A $aref = $AoA[$i];
1N/A $n = @$aref - 1;
1N/A for $j ( 0 .. $n ) {
1N/A print "elt $i $j is $AoA[$i][$j]\n";
1N/A }
1N/A }
1N/A
1N/A=head2 Slices
1N/A
1N/AIf you want to get at a slice (part of a row) in a multidimensional
1N/Aarray, you're going to have to do some fancy subscripting. That's
1N/Abecause while we have a nice synonym for single elements via the
1N/Apointer arrow for dereferencing, no such convenience exists for slices.
1N/A(Remember, of course, that you can always write a loop to do a slice
1N/Aoperation.)
1N/A
1N/AHere's how to do one operation using a loop. We'll assume an @AoA
1N/Avariable as before.
1N/A
1N/A @part = ();
1N/A $x = 4;
1N/A for ($y = 7; $y < 13; $y++) {
1N/A push @part, $AoA[$x][$y];
1N/A }
1N/A
1N/AThat same loop could be replaced with a slice operation:
1N/A
1N/A @part = @{ $AoA[4] } [ 7..12 ];
1N/A
1N/Abut as you might well imagine, this is pretty rough on the reader.
1N/A
1N/AAh, but what if you wanted a I<two-dimensional slice>, such as having
1N/A$x run from 4..8 and $y run from 7 to 12? Hmm... here's the simple way:
1N/A
1N/A @newAoA = ();
1N/A for ($startx = $x = 4; $x <= 8; $x++) {
1N/A for ($starty = $y = 7; $y <= 12; $y++) {
1N/A $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
1N/A }
1N/A }
1N/A
1N/AWe can reduce some of the looping through slices
1N/A
1N/A for ($x = 4; $x <= 8; $x++) {
1N/A push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
1N/A }
1N/A
1N/AIf you were into Schwartzian Transforms, you would probably
1N/Ahave selected map for that
1N/A
1N/A @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;
1N/A
1N/AAlthough if your manager accused of seeking job security (or rapid
1N/Ainsecurity) through inscrutable code, it would be hard to argue. :-)
1N/AIf I were you, I'd put that in a function:
1N/A
1N/A @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 );
1N/A sub splice_2D {
1N/A my $lrr = shift; # ref to array of array refs!
1N/A my ($x_lo, $x_hi,
1N/A $y_lo, $y_hi) = @_;
1N/A
1N/A return map {
1N/A [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ]
1N/A } $x_lo .. $x_hi;
1N/A }
1N/A
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/Aperldata(1), perlref(1), perldsc(1)
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ATom Christiansen <F<tchrist@perl.com>>
1N/A
1N/ALast update: Thu Jun 4 16:16:23 MDT 1998