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5 . 4 0 . 0 - R C 2
5 . 4 0 . 0 - R C 1
5 . 3 9 . 1 0
5 . 3 9 . 9
5 . 3 9 . 8
5 . 3 9 . 6
5 . 3 9 . 5
5 . 3 9 . 4
5 . 3 9 . 3
5 . 3 9 . 2
5 . 3 9 . 1
5 . 3 7 . 1 1
5 . 3 7 . 1 0
5 . 3 7 . 9
5 . 3 7 . 8
5 . 3 7 . 7
5 . 3 7 . 6
5 . 3 7 . 5
5 . 3 7 . 4
5 . 3 7 . 3
5 . 3 7 . 2
5 . 3 7 . 1
5 . 3 7 . 0
●
D o c u m e n t a t i o n
P e r l
I n t r o
T u t o r i a l s
F A Q s
R e f e r e n c e
O p e r a t o r s
F u n c t i o n s
V a r i a b l e s
M o d u l e s
U t i l i t i e s
C o m m u n i t y
H i s t o r y
E x p a n d
p e r l l o l
( s o u r c e ,
C P A N )
Y o u a r e v i e w i n g t h e v e r s i o n o f t h i s d o c u m e n t a t i o n f r o m P e r l 5 . 2 6 . 2 .
V i e w t h e l a t e s t v e r s i o n
●
N A M E
●
D E S C R I P T I O N
●
D e c l a r a t i o n a n d A c c e s s o f A r r a y s o f A r r a y s
●
G r o w i n g Y o u r O w n
●
A c c e s s a n d P r i n t i n g
●
S l i c e s
●
S E E A L S O
●
A U T H O R
# N A M E
p e r l l o l - M a n i p u l a t i n g A r r a y s o f A r r a y s i n P e r l
# D E S C R I P T I O N
# D e c l a r a t i o n a n d A c c e s s o f A r r a y s o f A r r a y s
T h e s i m p l e s t t w o - l e v e l d a t a s t r u c t u r e t o b u i l d i n P e r l i s a n a r r a y o f a r r a y s , s o m e t i m e s c a s u a l l y c a l l e d a l i s t o f l i s t s . I t ' s r e a s o n a b l y e a s y t o u n d e r s t a n d , a n d a l m o s t e v e r y t h i n g t h a t a p p l i e s h e r e w i l l a l s o b e a p p l i c a b l e l a t e r o n w i t h t h e f a n c i e r d a t a s t r u c t u r e s .
A n a r r a y o f a n a r r a y i s j u s t a r e g u l a r o l d a r r a y @ A o A t h a t y o u c a n g e t a t w i t h t w o s u b s c r i p t s , l i k e $ A o A [ 3 ] [ 2 ]
. H e r e ' s a d e c l a r a t i o n o f t h e a r r a y :
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
N o w y o u s h o u l d b e v e r y c a r e f u l t h a t t h e o u t e r b r a c k e t t y p e i s a r o u n d o n e , t h a t i s , a p a r e n t h e s i s . T h a t ' s b e c a u s e y o u ' r e a s s i g n i n g t o a n @ a r r a y , s o y o u n e e d p a r e n t h e s e s . I f y o u w a n t e d t h e r e n o t t o b e a n @ A o A , b u t r a t h e r j u s t a r e f e r e n c e t o i t , y o u c o u l d d o s o m e t h i n g m o r e l i k e t h i s :
# 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
N o t i c e t h a t t h e o u t e r b r a c k e t t y p e h a s c h a n g e d , a n d s o o u r a c c e s s s y n t a x h a s a l s o c h a n g e d . T h a t ' s b e c a u s e u n l i k e C , i n p e r l y o u c a n ' t f r e e l y i n t e r c h a n g e a r r a y s a n d r e f e r e n c e s t h e r e t o . $ r e f _ t o _ A o A i s a r e f e r e n c e t o a n a r r a y , w h e r e a s @ A o A i s a n a r r a y p r o p e r . L i k e w i s e , $ A o A [ 2 ]
i s n o t a n a r r a y , b u t a n a r r a y r e f . S o h o w c o m e y o u c a n w r i t e t h e s e :
$AoA[2 ][2 ]
$ref_to_AoA->[2 ][2 ]
i n s t e a d o f h a v i n g t o w r i t e t h e s e :
$AoA[2 ]->[2 ]
$ref_to_AoA->[2 ]->[2 ]
W e l l , t h a t ' s b e c a u s e t h e r u l e i s t h a t o n a d j a c e n t b r a c k e t s o n l y ( w h e t h e r s q u a r e o r c u r l y ) , y o u a r e f r e e t o o m i t t h e p o i n t e r d e r e f e r e n c i n g a r r o w . B u t y o u c a n n o t d o s o f o r t h e v e r y f i r s t o n e i f i t ' s a s c a l a r c o n t a i n i n g a r e f e r e n c e , w h i c h m e a n s t h a t $ r e f _ t o _ A o A a l w a y s n e e d s i t .
# G r o w i n g Y o u r O w n
T h a t ' s a l l w e l l a n d g o o d f o r d e c l a r a t i o n o f a f i x e d d a t a s t r u c t u r e , b u t w h a t i f y o u w a n t e d t o a d d n e w e l e m e n t s o n t h e f l y , o r b u i l d i t u p e n t i r e l y f r o m s c r a t c h ?
F i r s t , l e t ' s l o o k a t r e a d i n g i t i n f r o m a f i l e . T h i s i s s o m e t h i n g l i k e a d d i n g a r o w a t a t i m e . W e ' l l a s s u m e t h a t t h e r e ' s a f l a t f i l e i n w h i c h e a c h l i n e i s a r o w a n d e a c h w o r d a n e l e m e n t . I f y o u ' r e t r y i n g t o d e v e l o p a n @ A o A a r r a y c o n t a i n i n g a l l t h e s e , h e r e ' s t h e r i g h t w a y t o d o t h a t :
while (<>) {
@tmp = split;
push @AoA, [ @tmp ];
}
Y o u m i g h t a l s o h a v e l o a d e d t h a t f r o m a f u n c t i o n :
for $i ( 1 .. 10 ) {
$AoA[$i] = [ somefunc($i) ];
}
O r y o u m i g h t h a v e h a d a t e m p o r a r y v a r i a b l e s i t t i n g a r o u n d w i t h t h e a r r a y i n i t .
for $i ( 1 .. 10 ) {
@tmp = somefunc($i);
$AoA[$i] = [ @tmp ];
}
I t ' s i m p o r t a n t y o u m a k e s u r e t o u s e t h e [ ]
a r r a y r e f e r e n c e c o n s t r u c t o r . T h a t ' s b e c a u s e t h i s w o u l d n ' t w o r k :
$AoA[$i] = @tmp; # WRONG!
T h e r e a s o n t h a t d o e s n ' t d o w h a t y o u w a n t i s b e c a u s e a s s i g n i n g a n a m e d a r r a y l i k e t h a t t o a s c a l a r i s t a k i n g a n a r r a y i n s c a l a r c o n t e x t , w h i c h m e a n s j u s t c o u n t s t h e n u m b e r o f e l e m e n t s i n @ t m p .
I f y o u a r e r u n n i n g u n d e r u s e s t r
i c t
( a n d i f y o u a r e n ' t , w h y i n t h e w o r l d a r e n ' t y o u ? ) , y o u ' l l h a v e t o a d d s o m e d e c l a r a t i o n s t o m a k e i t h a p p y :
use strict;
my(@AoA, @tmp);
while (<>) {
@tmp = split;
push @AoA, [ @tmp ];
}
O f c o u r s e , y o u d o n ' t n e e d t h e t e m p o r a r y a r r a y t o h a v e a n a m e a t a l l :
while (<>) {
push @AoA, [ split ];
}
Y o u a l s o d o n ' t h a v e t o u s e p u s h ( ) . Y o u c o u l d j u s t m a k e a d i r e c t a s s i g n m e n t i f y o u k n e w w h e r e y o u w a n t e d t o p u t i t :
my (@AoA, $i, $line);
for $i ( 0 .. 10 ) {
$line = <>;
$AoA[$i] = [ split " ", $line ];
}
o r e v e n j u s t
my (@AoA, $i);
for $i ( 0 .. 10 ) {
$AoA[$i] = [ split " ", <> ];
}
Y o u s h o u l d i n g e n e r a l b e l e e r y o f u s i n g f u n c t i o n s t h a t c o u l d p o t e n t i a l l y r e t u r n l i s t s i n s c a l a r c o n t e x t w i t h o u t e x p l i c i t l y s t a t i n g s u c h . T h i s w o u l d b e c l e a r e r t o t h e c a s u a l r e a d e r :
my (@AoA, $i);
for $i ( 0 .. 10 ) {
$AoA[$i] = [ split " ", scalar(<>) ];
}
I f y o u w a n t e d t o h a v e a $ r e f _ t o _ A o A v a r i a b l e a s a r e f e r e n c e t o a n a r r a y , y o u ' d h a v e t o d o s o m e t h i n g l i k e t h i s :
while (<>) {
push @$ref_to_AoA, [ split ];
}
N o w y o u c a n a d d n e w r o w s . W h a t a b o u t a d d i n g n e w c o l u m n s ? I f y o u ' r e d e a l i n g w i t h j u s t m a t r i c e s , i t ' s o f t e n e a s i e s t t o u s e s i m p l e a s s i g n m e n t :
for $x (1 .. 10) {
for $y (1 .. 10) {
$AoA[$x][$y] = func($x, $y);
}
}
for $x ( 3, 7, 9 ) {
$AoA[$x][20 ] += func2($x);
}
I t d o e s n ' t m a t t e r w h e t h e r t h o s e e l e m e n t s a r e a l r e a d y t h e r e o r n o t : i t ' l l g l a d l y c r e a t e t h e m f o r y o u , s e t t i n g i n t e r v e n i n g e l e m e n t s t o u n d e f
a s n e e d b e .
I f y o u w a n t e d j u s t t o a p p e n d t o a r o w , y o u ' d h a v e t o d o s o m e t h i n g a b i t f u n n i e r l o o k i n g :
# add new columns to an existing row
push @{ $AoA[0] }, "wilma", "betty"; # explicit deref
# A c c e s s a n d P r i n t i n g
N o w i t ' s t i m e t o p r i n t y o u r d a t a s t r u c t u r e o u t . H o w a r e y o u g o i n g t o d o t h a t ? W e l l , i f y o u w a n t o n l y o n e o f t h e e l e m e n t s , i t ' s t r i v i a l :
print $AoA[0][0];
I f y o u w a n t t o p r i n t t h e w h o l e t h i n g , t h o u g h , y o u c a n ' t s a y
print @AoA; # WRONG
b e c a u s e y o u ' l l g e t j u s t r e f e r e n c e s l i s t e d , a n d p e r l w i l l n e v e r a u t o m a t i c a l l y d e r e f e r e n c e t h i n g s f o r y o u . I n s t e a d , y o u h a v e t o r o l l y o u r s e l f a l o o p o r t w o . T h i s p r i n t s t h e w h o l e s t r u c t u r e , u s i n g t h e s h e l l - s t y l e f o r ( ) c o n s t r u c t t o l o o p a c r o s s t h e o u t e r s e t o f s u b s c r i p t s .
for $aref ( @AoA ) {
say "\t [ @$aref ],";
}
I f y o u w a n t e d t o k e e p t r a c k o f s u b s c r i p t s , y o u m i g h t d o t h i s :
for $i ( 0 .. $#AoA ) {
say "\t elt $i is [ @{$AoA[$i]} ],";
}
o r m a y b e e v e n t h i s . N o t i c e t h e i n n e r l o o p .
for $i ( 0 .. $#AoA ) {
for $j ( 0 .. $#{$AoA[$i]} ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
A s y o u c a n s e e , i t ' s g e t t i n g a b i t c o m p l i c a t e d . T h a t ' s w h y s o m e t i m e s i s e a s i e r t o t a k e a t e m p o r a r y o n y o u r w a y t h r o u g h :
for $i ( 0 .. $#AoA ) {
$aref = $AoA[$i];
for $j ( 0 .. $#{$aref} ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
H m m . . . t h a t ' s s t i l l a b i t u g l y . H o w a b o u t t h i s :
for $i ( 0 .. $#AoA ) {
$aref = $AoA[$i];
$n = @$aref - 1;
for $j ( 0 .. $n ) {
say "elt $i $j is $AoA[$i][$j]";
}
}
W h e n y o u g e t t i r e d o f w r i t i n g a c u s t o m p r i n t f o r y o u r d a t a s t r u c t u r e s , y o u m i g h t l o o k a t t h e s t a n d a r d D u m p v a l u e or D a t a : : D u m p e r m o d u l e s . T h e f o r m e r i s w h a t t h e P e r l d e b u g g e r u s e s , w h i l e t h e l a t t e r g e n e r a t e s p a r s a b l e P e r l c o d e . F o r e x a m p l e :
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;
w i l l p r i n t o u t :
0 0..3 "fred" "barney" "wilma" "betty"
1 0..2 "george" "jane" "elroy"
2 0..2 "homer" "marge" "bart"
W h e r e a s i f y o u c o m m e n t o u t t h e t w o l i n e s I s a i d y o u m i g h t w i s h t o , t h e n i t s h o w s i t t o y o u t h i s w a y i n s t e a d :
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"
# S l i c e s
I f y o u w a n t t o g e t a t a s l i c e ( p a r t o f a r o w ) i n a m u l t i d i m e n s i o n a l a r r a y , y o u ' r e g o i n g t o h a v e t o d o s o m e f a n c y s u b s c r i p t i n g . T h a t ' s b e c a u s e w h i l e w e h a v e a n i c e s y n o n y m f o r s i n g l e e l e m e n t s v i a t h e p o i n t e r a r r o w f o r d e r e f e r e n c i n g , n o s u c h c o n v e n i e n c e e x i s t s f o r s l i c e s .
H e r e ' s h o w t o d o o n e o p e r a t i o n u s i n g a l o o p . W e ' l l a s s u m e a n @ A o A v a r i a b l e a s b e f o r e .
@part = ();
$x = 4;
for ($y = 7; $y < 13; $y++) {
push @part, $AoA[$x][$y];
}
T h a t s a m e l o o p c o u l d b e r e p l a c e d w i t h a s l i c e o p e r a t i o n :
@part = @{$AoA[4 ]}[7..12];
o r s p a c e d o u t a b i t :
@part = @{ $AoA[4 ] } [ 7..12 ];
B u t a s y o u m i g h t w e l l i m a g i n e , t h i s c a n g e t p r e t t y r o u g h o n t h e r e a d e r .
A h , b u t w h a t i f y o u w a n t e d a t w o - d i m e n s i o n a l s l i c e , s u c h a s h a v i n g $ x r u n f r o m 4 . . 8 a n d $ y r u n f r o m 7 t o 1 2 ? H m m . . . h e r e ' s t h e s i m p l e w a y :
@newAoA = ();
for ($startx = $x = 4; $x <= 8; $x++) {
for ($starty = $y = 7; $y <= 12; $y++) {
$newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
}
}
W e c a n r e d u c e s o m e o f t h e l o o p i n g t h r o u g h s l i c e s
for ($x = 4; $x <= 8; $x++) {
push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
}
I f y o u w e r e i n t o S c h w a r t z i a n T r a n s f o r m s , y o u w o u l d p r o b a b l y h a v e s e l e c t e d m a p f o r t h a t
@newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;
A l t h o u g h i f y o u r m a n a g e r a c c u s e d y o u o f s e e k i n g j o b s e c u r i t y ( o r r a p i d i n s e c u r i t y ) t h r o u g h i n s c r u t a b l e c o d e , i t w o u l d b e h a r d t o a r g u e . : - ) I f I w e r e y o u , I ' d p u t t h a t i n a f u n c t i o n :
@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;
}
# S E E A L S O
p e r l d a t a , p e r l r e f , p e r l d s c
# A U T H O R
T o m C h r i s t i a n s e n < t c h r i s t @ p e r l . c o m >
L a s t u p d a t e : T u e A p r 2 6 1 8 : 3 0 : 5 5 M D T 2 0 1 1
P e r l d o c B r o w s e r i s m a i n t a i n e d b y D a n B o o k ( D B O O K ) . P l e a s e c o n t a c t h i m v i a t h e G i t H u b i s s u e t r a c k e r or e m a i l r e g a r d i n g a n y i s s u e s w i t h t h e s i t e i t s e l f , s e a r c h , o r r e n d e r i n g o f d o c u m e n t a t i o n .
T h e P e r l d o c u m e n t a t i o n i s m a i n t a i n e d b y t h e P e r l 5 P o r t e r s i n t h e d e v e l o p m e n t o f P e r l . P l e a s e c o n t a c t t h e m v i a t h e P e r l i s s u e t r a c k e r , t h e m a i l i n g l i s t , o r I R C t o r e p o r t a n y i s s u e s w i t h t h e c o n t e n t s o r f o r m a t o f t h e d o c u m e n t a t i o n .