/
usr
/
share
/
perl5
/
pod
/
/usr/share/perl5/pod
mkdir
upload
Name
Size
Mode
Actions
perl.pod
16667
0644
edit
dl
rm
perl56delta.pod
107201
0644
edit
dl
rm
perl58delta.pod
115165
0644
edit
dl
rm
perl561delta.pod
124713
0644
edit
dl
rm
perl581delta.pod
38060
0644
edit
dl
rm
perl582delta.pod
4470
0644
edit
dl
rm
perl583delta.pod
6335
0644
edit
dl
rm
perl584delta.pod
7363
0644
edit
dl
rm
perl585delta.pod
5889
0644
edit
dl
rm
perl586delta.pod
4651
0644
edit
dl
rm
perl587delta.pod
8357
0644
edit
dl
rm
perl588delta.pod
25281
0644
edit
dl
rm
perl589delta.pod
53904
0644
edit
dl
rm
perl5004delta.pod
56240
0644
edit
dl
rm
perl5005delta.pod
34283
0644
edit
dl
rm
perl5100delta.pod
55533
0644
edit
dl
rm
perl5101delta.pod
43888
0644
edit
dl
rm
perl5120delta.pod
89271
0644
edit
dl
rm
perl5121delta.pod
10141
0644
edit
dl
rm
perl5122delta.pod
9603
0644
edit
dl
rm
perl5123delta.pod
4100
0644
edit
dl
rm
perl5124delta.pod
3672
0644
edit
dl
rm
perl5125delta.pod
7683
0644
edit
dl
rm
perl5140delta.pod
144325
0644
edit
dl
rm
perl5141delta.pod
7966
0644
edit
dl
rm
perl5142delta.pod
6892
0644
edit
dl
rm
perl5143delta.pod
7760
0644
edit
dl
rm
perl5144delta.pod
6327
0644
edit
dl
rm
perl5160delta.pod
133651
0644
edit
dl
rm
perl5161delta.pod
6142
0644
edit
dl
rm
perl5162delta.pod
3594
0644
edit
dl
rm
perl5163delta.pod
4085
0644
edit
dl
rm
perl5180delta.pod
119429
0644
edit
dl
rm
perl5181delta.pod
6597
0644
edit
dl
rm
perl5182delta.pod
5335
0644
edit
dl
rm
perl5184delta.pod
4642
0644
edit
dl
rm
perl5200delta.pod
115698
0644
edit
dl
rm
perl5201delta.pod
10899
0644
edit
dl
rm
perl5202delta.pod
12509
0644
edit
dl
rm
perl5203delta.pod
9392
0644
edit
dl
rm
perl5220delta.pod
130963
0644
edit
dl
rm
perl5221delta.pod
10767
0644
edit
dl
rm
perl5222delta.pod
12629
0644
edit
dl
rm
perl5223delta.pod
8456
0644
edit
dl
rm
perl5224delta.pod
4460
0644
edit
dl
rm
perl5240delta.pod
64923
0644
edit
dl
rm
perl5241delta.pod
8215
0644
edit
dl
rm
perl5242delta.pod
4113
0644
edit
dl
rm
perl5243delta.pod
11428
0644
edit
dl
rm
perl5244delta.pod
4510
0644
edit
dl
rm
perl5260delta.pod
101835
0644
edit
dl
rm
perl5261delta.pod
7927
0644
edit
dl
rm
perl5262delta.pod
7880
0644
edit
dl
rm
perl5263delta.pod
7067
0644
edit
dl
rm
perl5280delta.pod
72113
0644
edit
dl
rm
perl5281delta.pod
4639
0644
edit
dl
rm
perl5282delta.pod
6200
0644
edit
dl
rm
perl5283delta.pod
5111
0644
edit
dl
rm
perl5300delta.pod
41281
0644
edit
dl
rm
perl5301delta.pod
5730
0644
edit
dl
rm
perl5302delta.pod
4682
0644
edit
dl
rm
perl5303delta.pod
4980
0644
edit
dl
rm
perl5320delta.pod
54601
0644
edit
dl
rm
perl5321delta.pod
7003
0644
edit
dl
rm
perlaix.pod
20438
0644
edit
dl
rm
perlamiga.pod
5749
0644
edit
dl
rm
perlandroid.pod
8222
0644
edit
dl
rm
perlapi.pod
493106
0644
edit
dl
rm
perlapio.pod
21336
0644
edit
dl
rm
perlartistic.pod
7010
0644
edit
dl
rm
perlbook.pod
8386
0644
edit
dl
rm
perlboot.pod
294
0644
edit
dl
rm
perlbot.pod
304
0644
edit
dl
rm
perlbs2000.pod
7994
0644
edit
dl
rm
perlcall.pod
56738
0644
edit
dl
rm
perlcheat.pod
4480
0644
edit
dl
rm
perlclib.pod
9607
0644
edit
dl
rm
perlcn.pod
4763
0644
edit
dl
rm
perlcommunity.pod
6983
0644
edit
dl
rm
perlcygwin.pod
27202
0644
edit
dl
rm
perldata.pod
49782
0644
edit
dl
rm
perldbmfilter.pod
4981
0644
edit
dl
rm
perldebguts.pod
42331
0644
edit
dl
rm
perldebtut.pod
22152
0644
edit
dl
rm
perldebug.pod
39383
0644
edit
dl
rm
perldelta.pod
7003
0644
edit
dl
rm
perldeprecation.pod
20825
0644
edit
dl
rm
perldiag.pod
296986
0644
edit
dl
rm
perldos.pod
10522
0644
edit
dl
rm
perldsc.pod
25614
0644
edit
dl
rm
perldtrace.pod
7959
0644
edit
dl
rm
perlebcdic.pod
84345
0644
edit
dl
rm
perlembed.pod
37569
0644
edit
dl
rm
perlexperiment.pod
7234
0644
edit
dl
rm
perlfork.pod
13355
0644
edit
dl
rm
perlform.pod
16608
0644
edit
dl
rm
perlfreebsd.pod
1623
0644
edit
dl
rm
perlfunc.pod
403777
0644
edit
dl
rm
perlgit.pod
29591
0644
edit
dl
rm
perlgov.pod
14481
0644
edit
dl
rm
perlgpl.pod
13815
0644
edit
dl
rm
perlguts.pod
162103
0644
edit
dl
rm
perlhack.pod
43157
0644
edit
dl
rm
perlhacktips.pod
57692
0644
edit
dl
rm
perlhacktut.pod
6153
0644
edit
dl
rm
perlhaiku.pod
1508
0644
edit
dl
rm
perlhist.pod
56164
0644
edit
dl
rm
perlhpux.pod
30609
0644
edit
dl
rm
perlhurd.pod
1994
0644
edit
dl
rm
perlintern.pod
83565
0644
edit
dl
rm
perlinterp.pod
33715
0644
edit
dl
rm
perlintro.pod
22135
0644
edit
dl
rm
perliol.pod
34804
0644
edit
dl
rm
perlipc.pod
71529
0644
edit
dl
rm
perlirix.pod
4415
0644
edit
dl
rm
perljp.pod
8431
0644
edit
dl
rm
perlko.pod
12208
0644
edit
dl
rm
perllexwarn.pod
355
0644
edit
dl
rm
perllinux.pod
2007
0644
edit
dl
rm
perllocale.pod
74112
0644
edit
dl
rm
perllol.pod
9580
0644
edit
dl
rm
perlmacos.pod
998
0644
edit
dl
rm
perlmacosx.pod
12124
0644
edit
dl
rm
perlmod.pod
26197
0644
edit
dl
rm
perlmodinstall.pod
10261
0644
edit
dl
rm
perlmodlib.pod
76348
0644
edit
dl
rm
perlmodstyle.pod
22588
0644
edit
dl
rm
perlmroapi.pod
3280
0644
edit
dl
rm
perlnetware.pod
6648
0644
edit
dl
rm
perlnewmod.pod
11037
0644
edit
dl
rm
perlnumber.pod
8353
0644
edit
dl
rm
perlobj.pod
35717
0644
edit
dl
rm
perlootut.pod
26757
0644
edit
dl
rm
perlop.pod
148888
0644
edit
dl
rm
perlopenbsd.pod
1224
0644
edit
dl
rm
perlopentut.pod
15223
0644
edit
dl
rm
perlos2.pod
93348
0644
edit
dl
rm
perlos390.pod
16527
0644
edit
dl
rm
perlos400.pod
4765
0644
edit
dl
rm
perlpacktut.pod
51370
0644
edit
dl
rm
perlperf.pod
49881
0644
edit
dl
rm
perlplan9.pod
5125
0644
edit
dl
rm
perlpod.pod
23076
0644
edit
dl
rm
perlpodspec.pod
68628
0644
edit
dl
rm
perlpolicy.pod
26562
0644
edit
dl
rm
perlport.pod
88693
0644
edit
dl
rm
perlpragma.pod
5176
0644
edit
dl
rm
perlqnx.pod
6673
0644
edit
dl
rm
perlre.pod
130817
0644
edit
dl
rm
perlreapi.pod
30245
0644
edit
dl
rm
perlrebackslash.pod
32285
0644
edit
dl
rm
perlrecharclass.pod
49707
0644
edit
dl
rm
perlref.pod
35432
0644
edit
dl
rm
perlreftut.pod
18826
0644
edit
dl
rm
perlreguts.pod
39301
0644
edit
dl
rm
perlrepository.pod
509
0644
edit
dl
rm
perlrequick.pod
19141
0644
edit
dl
rm
perlreref.pod
15590
0644
edit
dl
rm
perlretut.pod
122527
0644
edit
dl
rm
perlriscos.pod
1529
0644
edit
dl
rm
perlrun.pod
55980
0644
edit
dl
rm
perlsec.pod
27601
0644
edit
dl
rm
perlsecpolicy.pod
19450
0644
edit
dl
rm
perlsolaris.pod
29843
0644
edit
dl
rm
perlsource.pod
7132
0644
edit
dl
rm
perlstyle.pod
8734
0644
edit
dl
rm
perlsub.pod
74405
0644
edit
dl
rm
perlsymbian.pod
15344
0644
edit
dl
rm
perlsyn.pod
45138
0644
edit
dl
rm
perlsynology.pod
7960
0644
edit
dl
rm
perlthrtut.pod
46522
0644
edit
dl
rm
perltie.pod
39314
0644
edit
dl
rm
perltoc.pod
701800
0644
edit
dl
rm
perltodo.pod
372
0644
edit
dl
rm
perltooc.pod
294
0644
edit
dl
rm
perltoot.pod
294
0644
edit
dl
rm
perltrap.pod
10623
0644
edit
dl
rm
perltru64.pod
8492
0644
edit
dl
rm
perltw.pod
4463
0644
edit
dl
rm
perlunicode.pod
91353
0644
edit
dl
rm
perlunicook.pod
25658
0644
edit
dl
rm
perlunifaq.pod
13644
0644
edit
dl
rm
perluniintro.pod
38647
0644
edit
dl
rm
perluniprops.pod
441614
0644
edit
dl
rm
perlunitut.pod
7951
0644
edit
dl
rm
perlutil.pod
7291
0644
edit
dl
rm
perlvar.pod
82577
0644
edit
dl
rm
perlvms.pod
50823
0644
edit
dl
rm
perlvos.pod
3843
0644
edit
dl
rm
perlwin32.pod
40317
0644
edit
dl
rm
Edit:
/usr/share/perl5/pod/perllol.pod
(9580B)
=head1 NAME perllol - Manipulating Arrays of Arrays in Perl =head1 DESCRIPTION =head2 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 C<$AoA[3][2]>. Here's a declaration of the array: use 5.010; # 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 I<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, C<$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. =head2 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 C<[ ]> 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 C<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 C<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 =head2 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 L<Dumpvalue> or L<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 $prettily = new Dumpvalue:: tick => q("), compactDump => 1, # comment these two lines # out veryCompact => 1, # if you want a bigger # dump ; dumpValue $prettily @_; } # 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" =head2 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]; or spaced out a bit: @part = @{ $AoA[4] } [ 7..12 ]; But as you might well imagine, this can get pretty rough on the reader. Ah, but what if you wanted a I<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; } =head1 SEE ALSO L<perldata>, L<perlref>, L<perldsc> =head1 AUTHOR Tom Christiansen <F<tchrist@perl.com>> Last update: Tue Apr 26 18:30:55 MDT 2011
Save
cmd:
run