Man page - perllol(1)
Packages contains this manual
- perlfaq2(1)
- perlvms(1)
- perlcn(1)
- perl5101delta(1)
- perl586delta(1)
- perlform(1)
- perlbook(1)
- perlpacktut(1)
- perlrequick(1)
- perlrecharclass(1)
- perlplan9(1)
- perl5303delta(1)
- perlipc(1)
- perlbot(1)
- perl5004delta(1)
- perlnumber(1)
- perltooc(1)
- perlxstypemap(1)
- perl5321delta(1)
- perlirix(1)
- perlmodlib(1)
- perlre(1)
- perlpolicy(1)
- perltoc(1)
- perlhacktips(1)
- perldebguts(1)
- perluniprops(1)
- perl5381delta(1)
- perldelta(1)
- perlcheat(1)
- perlebcdic(1)
- perl5301delta(1)
- perl5222delta(1)
- perlreapi(1)
- perlthrtut(1)
- perlandroid(1)
- perlref(1)
- perldsc(1)
- perlpodstyle(1)
- perlootut(1)
- perlfaq6(1)
- perlutil(1)
- perl5160delta(1)
- perlrun(1)
- perlsolaris(1)
- perltru64(1)
- perlcommunity(1)
- perlsynology(1)
- perl5184delta(1)
- perlhaiku(1)
- perl5120delta(1)
- perlartistic(1)
- perl585delta(1)
- perlstyle(1)
- perllocale(1)
- perl5300delta(1)
- perl5202delta(1)
- perlfaq(1)
- perlmod(1)
- perlgit(1)
- perl5140delta(1)
- perlos390(1)
- perllexwarn(1)
- perl5282delta(1)
- perl5142delta(1)
- perl5260delta(1)
- perldtrace(1)
- perldebtut(1)
- perl5243delta(1)
- perlgov(1)
- perlrebackslash(1)
- perl5220delta(1)
- perlfaq5(1)
- perlguts(1)
- perl5382delta(1)
- perl5281delta(1)
- perlfaq1(1)
- perlperf(1)
- perlmodstyle(1)
- perlbs2000(1)
- perldiag(1)
- perlmroapi(1)
- perldata(1)
- perlos400(1)
- perl5341delta(1)
- perl5181delta(1)
- perlretut(1)
- perlnewmod(1)
- perl584delta(1)
- perlsec(1)
- perlport(1)
- perlfaq8(1)
- perlunitut(1)
- perlko(1)
- perlop(1)
- perllol(1)
- perltw(1)
- perl5263delta(1)
- perlopentut(1)
- perlfaq3(1)
- perldebug(1)
- perlgpl(1)
- perlintro(1)
- perldoc(1)
- perlfreebsd(1)
- perl5340delta(1)
- perlapi(1)
- perl5221delta(1)
- perlembed(1)
- perl5362delta(1)
- perlreguts(1)
- perl5283delta(1)
- perl5400delta(1)
- perlfaq7(1)
- perl5361delta(1)
- perl5380delta(1)
- perlunifaq(1)
- perlxs(1)
- perltoot(1)
- perlsub(1)
- perlhpux(1)
- perltie(1)
- perl588delta(1)
- perl581delta(1)
- perlobj(1)
- perlapio(1)
- perlxstut(1)
- perllinux(1)
- perlhurd(1)
- perl5203delta(1)
- perl587delta(1)
- perlcygwin(1)
- perlos2(1)
- perl5320delta(1)
- perlaix(1)
- perlunicode(1)
- perl5360delta(1)
- perl5161delta(1)
- perlhack(1)
- perltrap(1)
- perl5100delta(1)
- perl5182delta(1)
- perlfunc(1)
- perlexperiment(1)
- perl5261delta(1)
- perl589delta(1)
- perlfork(1)
- perlintern(1)
- perl5123delta(1)
- perluniintro(1)
- perlsecpolicy(1)
- perl5124delta(1)
- perl5121delta(1)
- perlsource(1)
- perl5200delta(1)
- perlcall(1)
- perlmacosx(1)
- perl5262delta(1)
- perl58delta(1)
- perldbmfilter(1)
- perlrepository(1)
- perl5241delta(1)
- perlunicook(1)
- perlfaq4(1)
- perliol(1)
- perlpodspec(1)
- perlclassguts(1)
- perltodo(1)
- perl5223delta(1)
- perl56delta(1)
- perlreref(1)
- perlinterp(1)
- perlopenbsd(1)
- perlmodinstall(1)
- perl5144delta(1)
- perlclass(1)
- perlvar(1)
- perl5180delta(1)
- perlpragma(1)
- perljp(1)
- perl5242delta(1)
- perlpod(1)
- perlvos(1)
- perlriscos(1)
- perldeprecation(1)
- perl5125delta(1)
- perlhist(1)
- perl5343delta(1)
- perl561delta(1)
- perl5224delta(1)
- perlamiga(1)
- perlclib(1)
- perlsyn(1)
- perl5280delta(1)
- perlfaq9(1)
- perlwin32(1)
- perl5240delta(1)
- perl5244delta(1)
- perlglossary(1)
- perl5342delta(1)
- perl5143delta(1)
- perl5302delta(1)
- perl5201delta(1)
- perlboot(1)
- perl5162delta(1)
- perlqnx(1)
- perldocstyle(1)
- perl5401delta(1)
- perlhacktut(1)
- perl5363delta(1)
- perl5005delta(1)
- perl583delta(1)
- perl5141delta(1)
- perlreftut(1)
- perl582delta(1)
- perl5163delta(1)
- perlfilter(1)
- perl5122delta(1)
apt-get install perl-doc
Manual
PERLLOL
NAMEDESCRIPTION
Declaration and Access of Arrays of Arrays
Growing Your Own
Access and Printing
Slices
SEE ALSO
AUTHOR
NAME
perllol - Manipulating Arrays of Arrays in Perl
DESCRIPTION
Declaration and Access of Arrays of Arrays
The simplest two-level data structure to build in Perl is an array of arrays, sometimes casually 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 array:
use v5.10; # so
we can use say()
# assign to our array, an array of array references
@AoA = (
[ "fred", "barney", "pebbles",
"bambam", "dino", ],
[ "george", "jane", "elroy",
"judy", ],
[ "homer", "bart", "marge",
"maggie", ],
);
say $AoA[2][1];
bart
Now you should be very careful that the outer bracket type is a round one, that is, a parenthesis. Thatâs because youâre assigning to an @array, so you need parentheses. If you wanted there not to be an @AoA, but rather just a reference to it, you could do something more like this:
# assign a
reference to array of array references
$ref_to_AoA = [
[ "fred", "barney", "pebbles",
"bambam", "dino", ],
[ "george", "jane", "elroy",
"judy", ],
[ "homer", "bart", "marge",
"maggie", ],
];
say $ref_to_AoA->[2][1];
bart
Notice that the outer bracket type has changed, and so our access syntax has also changed. Thatâs because unlike C, in perl you canât freely interchange arrays and references thereto. $ref_to_AoA is a reference to an array, whereas @AoA is an array proper. Likewise, $AoA[2] is not an array, but an array ref. So how come you can write these:
$AoA[2][2]
$ref_to_AoA->[2][2]
instead of having to write these:
$AoA[2]->[2]
$ref_to_AoA->[2]->[2]
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 way to do that:
while (<>)
{
@tmp = split;
push @AoA, [ @tmp ];
}
You might also have loaded that from a function:
for $i ( 1 .. 10
) {
$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 ];
}
Itâs important you make sure to use the "[ ]" array reference constructor. Thatâs because this wouldnât work:
$AoA[$i] = @tmp; # WRONG!
The reason that doesnât do what you want is because assigning a named array like that to a scalar is taking an array in scalar context, which means just counts the number of elements in @tmp.
If you are running under "use strict" (and if you arenât, why in the world arenât you?), youâll have to add some declarations to make it happy:
use strict;
my(@AoA, @tmp);
while (<>) {
@tmp = split;
push @AoA, [ @tmp ];
}
Of course, you donât need the temporary array to have a name at all:
while (<>)
{
push @AoA, [ split ];
}
You also donât have to use push() . You could just make a direct assignment if you knew where you wanted to put it:
my (@AoA, $i,
$line);
for $i ( 0 .. 10 ) {
$line = <>;
$AoA[$i] = [ split " ", $line ];
}
or even just
my (@AoA, $i);
for $i ( 0 .. 10 ) {
$AoA[$i] = [ split " ", <> ];
}
You should in general be leery of using functions that could potentially return lists in scalar context without explicitly stating such. This would be clearer to the casual reader:
my (@AoA, $i);
for $i ( 0 .. 10 ) {
$AoA[$i] = [ split " ", scalar(<>) ];
}
If you wanted to have a $ref_to_AoA variable as a reference to an array, youâd have to do something like this:
while (<>)
{
push @$ref_to_AoA, [ split ];
}
Now you can add new rows. What about adding new columns? If youâre dealing with just matrices, itâs often easiest to use simple assignment:
for $x (1 .. 10)
{
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 something a bit funnier looking:
# add new
columns to an existing row
push $AoA[0]->@*, "wilma", "betty"; #
explicit deref
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 elements, itâs trivial:
print $AoA[0][0];
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
) {
say "\t [ @$aref ],";
}
If you wanted to keep track of subscripts, you might do this:
for $i ( 0 ..
$#AoA ) {
say "\t elt $i is [ @{$AoA[$i]} ],";
}
or maybe even this. Notice the inner loop.
for $i ( 0 ..
$#AoA ) {
for $j ( 0 .. $#{$AoA[$i]} ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
As you can see, itâs getting a bit complicated. Thatâs why sometimes is easier to take a temporary on your way through:
for $i ( 0 ..
$#AoA ) {
$aref = $AoA[$i];
for $j ( 0 .. $#{$aref} ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
Hmm... thatâs still a bit ugly. How about this:
for $i ( 0 ..
$#AoA ) {
$aref = $AoA[$i];
$n = @$aref - 1;
for $j ( 0 .. $n ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
When you get tired of writing a custom "print" for your data structures, you might look at the standard Dumpvalue or Data::Dumper modules. The former is what the Perl debugger uses, while the latter generates parsable Perl code. For example:
use v5.14; #
using the + prototype, new to v5.14
sub show(+) {
require Dumpvalue;
state $prettify = Dumpvalue->new(
tick => q("),
compactDump => 1, # comment these two lines out
veryCompact => 1, # if you want a bigger dump
);
$prettify->dumpValue(@_);
}
# Assign a list of array references to an array.
my @AoA = (
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
);
push $AoA[0]->@*, "wilma", "betty";
show @AoA;
will print out:
0 0..3
"fred" "barney" "wilma"
"betty"
1 0..2 "george" "jane" "elroy"
2 0..2 "homer" "marge"
"bart"
Whereas if you comment out the two lines I said you might wish to, then it shows it to you this way instead:
0
ARRAY(0x8031d0)
0 "fred"
1 "barney"
2 "wilma"
3 "betty"
1 ARRAY(0x803d40)
0 "george"
1 "jane"
2 "elroy"
2 ARRAY(0x803e10)
0 "homer"
1 "marge"
2 "bart"
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.
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 < 13; $y++) {
push @part, $AoA[$x][$y];
}
That same loop could be replaced with a slice operation:
@part = $AoA[4]->@[ 7..12 ];
Now, what if you wanted a two-dimensional slice , such as having $x run from 4..8 and $y run from 7 to 12? Hmm... hereâs the simple way:
@newAoA = ();
for ($startx = $x = 4; $x <= 8; $x++) {
for ($starty = $y = 7; $y <= 12; $y++) {
$newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
}
}
We can reduce some of the looping through slices
for ($x = 4; $x
<= 8; $x++) {
push @newAoA, [ $AoA[$x]->@[ 7..12 ] ];
}
If you were into Schwartzian Transforms, you would probably have selected map for that
@newAoA = map { [ $AoA[$_]->@[ 7..12 ] ] } 4 .. 8;
Although if your manager accused you of seeking job security (or rapid insecurity) through inscrutable code, it would be hard to argue. :-) If I were you, Iâd put that in a function:
@newAoA =
splice_2D( \@AoA, 4 => 8, 7 => 12 );
sub splice_2D {
my $lrr = shift; # ref to array of array refs!
my ($x_lo, $x_hi,
$y_lo, $y_hi) = @_;
return map {
[ $lrr->[$_]->@[ $y_lo .. $y_hi ] ]
} $x_lo .. $x_hi;
}
SEE ALSO
perldata, perlref, perldsc
AUTHOR
Tom Christiansen < tchrist@perl.com >
Last update: Tue Apr 26 18:30:55 MDT 2011