@@ -9,12 +9,12 @@ Perl lets us have complex data structures. You can write something like
9
9
this and all of a sudden, you'd have an array with three dimensions!
10
10
11
11
for $x (1 .. 10) {
12
- for $y (1 .. 10) {
13
- for $z (1 .. 10) {
14
- $AoA[$x][$y][$z] =
15
- $x ** $y + $z;
16
- }
17
- }
12
+ for $y (1 .. 10) {
13
+ for $z (1 .. 10) {
14
+ $AoA[$x][$y][$z] =
15
+ $x ** $y + $z;
16
+ }
17
+ }
18
18
}
19
19
20
20
Alas, however simple this may appear, underneath it's a much more
@@ -85,10 +85,10 @@ level. It's just that you can I<use> it as though it were a
85
85
two-dimensional one. This is actually the way almost all C
86
86
multidimensional arrays work as well.
87
87
88
- $array[7][12] # array of arrays
89
- $array[7]{string} # array of hashes
90
- $hash{string}[7] # hash of arrays
91
- $hash{string}{'another string'} # hash of hashes
88
+ $array[7][12] # array of arrays
89
+ $array[7]{string} # array of hashes
90
+ $hash{string}[7] # hash of arrays
91
+ $hash{string}{'another string'} # hash of hashes
92
92
93
93
Now, because the top level contains only references, if you try to print
94
94
out your array in with a simple print() function, you'll get something
@@ -116,25 +116,25 @@ repeatedly. Here's the case where you just get the count instead
116
116
of a nested array:
117
117
118
118
for $i (1..10) {
119
- @array = somefunc($i);
120
- $AoA[$i] = @array; # WRONG!
119
+ @array = somefunc($i);
120
+ $AoA[$i] = @array; # WRONG!
121
121
}
122
122
123
123
That's just the simple case of assigning an array to a scalar and getting
124
124
its element count. If that's what you really and truly want, then you
125
125
might do well to consider being a tad more explicit about it, like this:
126
126
127
127
for $i (1..10) {
128
- @array = somefunc($i);
129
- $counts[$i] = scalar @array;
128
+ @array = somefunc($i);
129
+ $counts[$i] = scalar @array;
130
130
}
131
131
132
132
Here's the case of taking a reference to the same memory location
133
133
again and again:
134
134
135
135
for $i (1..10) {
136
- @array = somefunc($i);
137
- $AoA[$i] = \@array; # WRONG!
136
+ @array = somefunc($i);
137
+ $AoA[$i] = \@array; # WRONG!
138
138
}
139
139
140
140
So, what's the big problem with that? It looks right, doesn't it?
@@ -148,12 +148,12 @@ the following C program:
148
148
149
149
#include <pwd.h>
150
150
main() {
151
- struct passwd *getpwnam(), *rp, *dp;
152
- rp = getpwnam("root");
153
- dp = getpwnam("daemon");
151
+ struct passwd *getpwnam(), *rp, *dp;
152
+ rp = getpwnam("root");
153
+ dp = getpwnam("daemon");
154
154
155
- printf("daemon name is %s\nroot name is %s\n",
156
- dp->pw_name, rp->pw_name);
155
+ printf("daemon name is %s\nroot name is %s\n",
156
+ dp->pw_name, rp->pw_name);
157
157
}
158
158
159
159
Which will print
@@ -169,8 +169,8 @@ broken code fragments:
169
169
X<[]> X<{}>
170
170
171
171
for $i (1..10) {
172
- @array = somefunc($i);
173
- $AoA[$i] = [ @array ];
172
+ @array = somefunc($i);
173
+ $AoA[$i] = [ @array ];
174
174
}
175
175
176
176
The square brackets make a reference to a new array with a I<copy>
@@ -181,8 +181,8 @@ Note that this will produce something similar, but it's
181
181
much harder to read:
182
182
183
183
for $i (1..10) {
184
- @array = 0 .. $i;
185
- @{$AoA[$i]} = @array;
184
+ @array = 0 .. $i;
185
+ @{$AoA[$i]} = @array;
186
186
}
187
187
188
188
Is it the same? Well, maybe so--and maybe not. The subtle difference
@@ -235,9 +235,9 @@ do the right thing behind the scenes.
235
235
236
236
In summary:
237
237
238
- $AoA[$i] = [ @array ]; # usually best
239
- $AoA[$i] = \@array; # perilous; just how my() was that array?
240
- @{ $AoA[$i] } = @array; # way too tricky for most programmers
238
+ $AoA[$i] = [ @array ]; # usually best
239
+ $AoA[$i] = \@array; # perilous; just how my() was that array?
240
+ @{ $AoA[$i] } = @array; # way too tricky for most programmers
241
241
242
242
243
243
=head1 CAVEAT ON PRECEDENCE
@@ -247,8 +247,8 @@ Speaking of things like C<@{$AoA[$i]}>, the following are actually the
247
247
same thing:
248
248
X<< -> >>
249
249
250
- $aref->[2][2] # clear
251
- $$aref[2][2] # confusing
250
+ $aref->[2][2] # clear
251
+ $$aref[2][2] # confusing
252
252
253
253
That's because Perl's precedence rules on its five prefix dereferencers
254
254
(which look like someone swearing: C<$ @ * % &>) make them bind more
@@ -279,9 +279,9 @@ also disallow accidental "symbolic dereferencing". Therefore if you'd done
279
279
this:
280
280
281
281
my $aref = [
282
- [ "fred", "barney", "pebbles", "bambam", "dino", ],
283
- [ "homer", "bart", "marge", "maggie", ],
284
- [ "george", "jane", "elroy", "judy", ],
282
+ [ "fred", "barney", "pebbles", "bambam", "dino", ],
283
+ [ "homer", "bart", "marge", "maggie", ],
284
+ [ "george", "jane", "elroy", "judy", ],
285
285
];
286
286
287
287
print $aref[2][2];
@@ -304,21 +304,21 @@ For example, given the assignment to $AoA above, here's the debugger output:
304
304
DB<1> x $AoA
305
305
$AoA = ARRAY(0x13b5a0)
306
306
0 ARRAY(0x1f0a24)
307
- 0 'fred'
308
- 1 'barney'
309
- 2 'pebbles'
310
- 3 'bambam'
311
- 4 'dino'
307
+ 0 'fred'
308
+ 1 'barney'
309
+ 2 'pebbles'
310
+ 3 'bambam'
311
+ 4 'dino'
312
312
1 ARRAY(0x13b558)
313
- 0 'homer'
314
- 1 'bart'
315
- 2 'marge'
316
- 3 'maggie'
313
+ 0 'homer'
314
+ 1 'bart'
315
+ 2 'marge'
316
+ 3 'maggie'
317
317
2 ARRAY(0x13b540)
318
- 0 'george'
319
- 1 'jane'
320
- 2 'elroy'
321
- 3 'judy'
318
+ 0 'george'
319
+ 1 'jane'
320
+ 2 'elroy'
321
+ 3 'judy'
322
322
323
323
=head1 CODE EXAMPLES
324
324
@@ -454,10 +454,10 @@ X<hash of arrays> X<HoA>
454
454
455
455
# print the whole thing sorted by number of members and name
456
456
foreach $family ( sort {
457
- @{$HoA{$b}} <=> @{$HoA{$a}}
458
- ||
459
- $a cmp $b
460
- } keys %HoA )
457
+ @{$HoA{$b}} <=> @{$HoA{$a}}
458
+ ||
459
+ $a cmp $b
460
+ } keys %HoA )
461
461
{
462
462
print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";
463
463
}
@@ -560,19 +560,19 @@ X<hash of hashes> X<HoH>
560
560
561
561
%HoH = (
562
562
flintstones => {
563
- lead => "fred",
564
- pal => "barney",
563
+ lead => "fred",
564
+ pal => "barney",
565
565
},
566
566
jetsons => {
567
- lead => "george",
568
- wife => "jane",
569
- "his boy" => "elroy",
567
+ lead => "george",
568
+ wife => "jane",
569
+ "his boy" => "elroy",
570
570
},
571
571
simpsons => {
572
- lead => "homer",
573
- wife => "marge",
574
- kid => "bart",
575
- },
572
+ lead => "homer",
573
+ wife => "marge",
574
+ kid => "bart",
575
+ },
576
576
);
577
577
578
578
=head2 Generation of a HASH OF HASHES
@@ -681,12 +681,12 @@ Here's a sample showing how to create and use a record whose fields are of
681
681
many different sorts:
682
682
683
683
$rec = {
684
- TEXT => $string,
685
- SEQUENCE => [ @old_values ],
686
- LOOKUP => { %some_table },
687
- THATCODE => \&some_function,
688
- THISCODE => sub { $_[0] ** $_[1] },
689
- HANDLE => \*STDOUT,
684
+ TEXT => $string,
685
+ SEQUENCE => [ @old_values ],
686
+ LOOKUP => { %some_table },
687
+ THATCODE => \&some_function,
688
+ THISCODE => sub { $_[0] ** $_[1] },
689
+ HANDLE => \*STDOUT,
690
690
};
691
691
692
692
print $rec->{TEXT};
0 commit comments