PERLLOL
NAME DESCRIPTION SEE ALSO AUTHOR
perllol - Manipulating Arrays of Arrays in Perl
Declaration and Access of Arrays of Arrays
The simplest thing to build an array of arrays (sometimes imprecisely called a list of lists). It's reasonably easy to understand, and almost everything that applies here will also be applicable later on with the fancier data structures.
An array of an array is just a regular old array @AoA that you can get at with two subscripts, like $AoA[3?[2?. Here's a declaration of the
@AoA = ( [[
bart
$ref_to_AoA = [[ [[
print $ref_to_AoA-
$ref_to_AoA-
Well, that's because the rule is that on adjacent brackets only (whether square or curly), you are free to omit the pointer dereferencing arrow. But you cannot do so for the very first one if it's a scalar containing a reference, which means that $ref_to_AoA always needs it.
Growing Your Own
That's all well and good for declaration of a fixed data structure, but what if you wanted to add new elements on the fly, or build it up entirely from scratch?
First, let's look at reading it in from a file. This is something like adding a row at a time. We'll assume that there's a flat file in which each line is a row and each word an element. If you're trying to develop an @AoA array containing all these, here's the right
$AoA[$i? = [ somefunc($i)?; } Or you might have had a temporary variable sitting around with the array in it.
for $i ( 1 .. 10 ) {
@tmp = somefunc($i); $AoA[$i? = [ @tmp?; }
You see, assigning a named array like that to a scalar just counts the number of elements in @tmp, which probably isn't what you want.
If you are running under use strict, you'll have to
my(@AoA, @tmp); while (
for $i ( 0 .. 10 ) { $line = or even just
my (@AoA, $i);
for $i ( 0 .. 10 ) { $AoA[$i? = [[ split ' ',
for $i ( 0 .. 10 ) { $AoA[$i? = [[ split ' ', scalar(
for $y (1 .. 10) { $AoA[$x?[$y? = func($x, $y); } }
for $x ( 3, 7, 9 ) {
$AoA[$x?[20? += func2($x); } It doesn't matter whether those elements are already there or not: it'll gladly create them for you, setting intervening elements to undef as need be.
If you wanted just to append to a row, you'd have to do
push @{ $AoA[0? },
In fact, that wouldn't even compile. How come? Because the argument to push() must be a real array, not just a reference to such.
Access and Printing
Now it's time to print your data structure out. How are you going to do that? Well, if you want only one of the
If you want to print the whole thing, though, you can't say
print @AoA; # WRONG
because you'll get just references listed, and perl will never automatically dereference things for you. Instead, you have to roll yourself a loop or two. This prints the whole structure, using the shell-style for() construct to loop across the outer set of subscripts.
for $aref ( @AoA ) {
print or maybe even this. Notice the inner loop.
for $i ( 0 .. $#AoA ) {
for $j ( 0 .. $#{$AoA[$i?} ) { print
$aref = $AoA[$i?; for $j ( 0 .. $#{$aref} ) { print
$aref = $AoA[$i?; $n = @$aref - 1; for $j ( 0 .. $n ) { print
Slices
If you want to get at a slice (part of a row) in a multidimensional array, you're going to have to do some fancy subscripting. That's because while we have a nice synonym for single elements via the pointer arrow for dereferencing, no such convenience exists for slices. (Remember, of course, that you can always write a loop to do a slice operation.)
Here's how to do one operation using a loop. We'll assume an @AoA variable as before.
@part = ();
$x = 4; for ($y = 7; $y
but as you might well imagine, this is pretty rough on the reader.
Ah, but what if you wanted a two-dimensional slice, such as having $x run from 4..8 and $y run
for ($startx = $x = 4; $x We can reduce some of the looping through slices
for ($x = 4; $x
If you were into Schwartzian Transforms, you would probably have selected map for that
@newAoA = splice_2D( @AoA, 4 =
return map {
[[ @{ $lrr-
perldata(1), perlref(1), perldsc(1)
Tom Christiansen tchrist@perl.com
Last update: Thu Jun 4 16:16:23 MDT 1998
2 pages link to perllol(1):