| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLLOL 1" |
| 132 | .TH PERLLOL 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perllol \- Manipulating Arrays of Arrays in Perl |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | .Sh "Declaration and Access of Arrays of Arrays" |
| 138 | .IX Subsection "Declaration and Access of Arrays of Arrays" |
| 139 | The simplest thing to build is an array of arrays (sometimes imprecisely |
| 140 | called a list of lists). It's reasonably easy to understand, and |
| 141 | almost everything that applies here will also be applicable later |
| 142 | on with the fancier data structures. |
| 143 | .PP |
| 144 | An array of an array is just a regular old array \f(CW@AoA\fR that you can |
| 145 | get at with two subscripts, like \f(CW$AoA[3][2]\fR. Here's a declaration |
| 146 | of the array: |
| 147 | .PP |
| 148 | .Vb 6 |
| 149 | \& # assign to our array, an array of array references |
| 150 | \& @AoA = ( |
| 151 | \& [ "fred", "barney" ], |
| 152 | \& [ "george", "jane", "elroy" ], |
| 153 | \& [ "homer", "marge", "bart" ], |
| 154 | \& ); |
| 155 | .Ve |
| 156 | .PP |
| 157 | .Vb 2 |
| 158 | \& print $AoA[2][2]; |
| 159 | \& bart |
| 160 | .Ve |
| 161 | .PP |
| 162 | Now you should be very careful that the outer bracket type |
| 163 | is a round one, that is, a parenthesis. That's because you're assigning to |
| 164 | an \f(CW@array\fR, so you need parentheses. If you wanted there \fInot\fR to be an \f(CW@AoA\fR, |
| 165 | but rather just a reference to it, you could do something more like this: |
| 166 | .PP |
| 167 | .Vb 6 |
| 168 | \& # assign a reference to array of array references |
| 169 | \& $ref_to_AoA = [ |
| 170 | \& [ "fred", "barney", "pebbles", "bambam", "dino", ], |
| 171 | \& [ "homer", "bart", "marge", "maggie", ], |
| 172 | \& [ "george", "jane", "elroy", "judy", ], |
| 173 | \& ]; |
| 174 | .Ve |
| 175 | .PP |
| 176 | .Vb 1 |
| 177 | \& print $ref_to_AoA->[2][2]; |
| 178 | .Ve |
| 179 | .PP |
| 180 | Notice that the outer bracket type has changed, and so our access syntax |
| 181 | has also changed. That's because unlike C, in perl you can't freely |
| 182 | interchange arrays and references thereto. \f(CW$ref_to_AoA\fR is a reference to an |
| 183 | array, whereas \f(CW@AoA\fR is an array proper. Likewise, \f(CW$AoA[2]\fR is not an |
| 184 | array, but an array ref. So how come you can write these: |
| 185 | .PP |
| 186 | .Vb 2 |
| 187 | \& $AoA[2][2] |
| 188 | \& $ref_to_AoA->[2][2] |
| 189 | .Ve |
| 190 | .PP |
| 191 | instead of having to write these: |
| 192 | .PP |
| 193 | .Vb 2 |
| 194 | \& $AoA[2]->[2] |
| 195 | \& $ref_to_AoA->[2]->[2] |
| 196 | .Ve |
| 197 | .PP |
| 198 | Well, that's because the rule is that on adjacent brackets only (whether |
| 199 | square or curly), you are free to omit the pointer dereferencing arrow. |
| 200 | But you cannot do so for the very first one if it's a scalar containing |
| 201 | a reference, which means that \f(CW$ref_to_AoA\fR always needs it. |
| 202 | .Sh "Growing Your Own" |
| 203 | .IX Subsection "Growing Your Own" |
| 204 | That's all well and good for declaration of a fixed data structure, |
| 205 | but what if you wanted to add new elements on the fly, or build |
| 206 | it up entirely from scratch? |
| 207 | .PP |
| 208 | First, let's look at reading it in from a file. This is something like |
| 209 | adding a row at a time. We'll assume that there's a flat file in which |
| 210 | each line is a row and each word an element. If you're trying to develop an |
| 211 | \&\f(CW@AoA\fR array containing all these, here's the right way to do that: |
| 212 | .PP |
| 213 | .Vb 4 |
| 214 | \& while (<>) { |
| 215 | \& @tmp = split; |
| 216 | \& push @AoA, [ @tmp ]; |
| 217 | \& } |
| 218 | .Ve |
| 219 | .PP |
| 220 | You might also have loaded that from a function: |
| 221 | .PP |
| 222 | .Vb 3 |
| 223 | \& for $i ( 1 .. 10 ) { |
| 224 | \& $AoA[$i] = [ somefunc($i) ]; |
| 225 | \& } |
| 226 | .Ve |
| 227 | .PP |
| 228 | Or you might have had a temporary variable sitting around with the |
| 229 | array in it. |
| 230 | .PP |
| 231 | .Vb 4 |
| 232 | \& for $i ( 1 .. 10 ) { |
| 233 | \& @tmp = somefunc($i); |
| 234 | \& $AoA[$i] = [ @tmp ]; |
| 235 | \& } |
| 236 | .Ve |
| 237 | .PP |
| 238 | It's very important that you make sure to use the \f(CW\*(C`[]\*(C'\fR array reference |
| 239 | constructor. That's because this will be very wrong: |
| 240 | .PP |
| 241 | .Vb 1 |
| 242 | \& $AoA[$i] = @tmp; |
| 243 | .Ve |
| 244 | .PP |
| 245 | You see, assigning a named array like that to a scalar just counts the |
| 246 | number of elements in \f(CW@tmp\fR, which probably isn't what you want. |
| 247 | .PP |
| 248 | If you are running under \f(CW\*(C`use strict\*(C'\fR, you'll have to add some |
| 249 | declarations to make it happy: |
| 250 | .PP |
| 251 | .Vb 6 |
| 252 | \& use strict; |
| 253 | \& my(@AoA, @tmp); |
| 254 | \& while (<>) { |
| 255 | \& @tmp = split; |
| 256 | \& push @AoA, [ @tmp ]; |
| 257 | \& } |
| 258 | .Ve |
| 259 | .PP |
| 260 | Of course, you don't need the temporary array to have a name at all: |
| 261 | .PP |
| 262 | .Vb 3 |
| 263 | \& while (<>) { |
| 264 | \& push @AoA, [ split ]; |
| 265 | \& } |
| 266 | .Ve |
| 267 | .PP |
| 268 | You also don't have to use \fIpush()\fR. You could just make a direct assignment |
| 269 | if you knew where you wanted to put it: |
| 270 | .PP |
| 271 | .Vb 5 |
| 272 | \& my (@AoA, $i, $line); |
| 273 | \& for $i ( 0 .. 10 ) { |
| 274 | \& $line = <>; |
| 275 | \& $AoA[$i] = [ split ' ', $line ]; |
| 276 | \& } |
| 277 | .Ve |
| 278 | .PP |
| 279 | or even just |
| 280 | .PP |
| 281 | .Vb 4 |
| 282 | \& my (@AoA, $i); |
| 283 | \& for $i ( 0 .. 10 ) { |
| 284 | \& $AoA[$i] = [ split ' ', <> ]; |
| 285 | \& } |
| 286 | .Ve |
| 287 | .PP |
| 288 | You should in general be leery of using functions that could |
| 289 | potentially return lists in scalar context without explicitly stating |
| 290 | such. This would be clearer to the casual reader: |
| 291 | .PP |
| 292 | .Vb 4 |
| 293 | \& my (@AoA, $i); |
| 294 | \& for $i ( 0 .. 10 ) { |
| 295 | \& $AoA[$i] = [ split ' ', scalar(<>) ]; |
| 296 | \& } |
| 297 | .Ve |
| 298 | .PP |
| 299 | If you wanted to have a \f(CW$ref_to_AoA\fR variable as a reference to an array, |
| 300 | you'd have to do something like this: |
| 301 | .PP |
| 302 | .Vb 3 |
| 303 | \& while (<>) { |
| 304 | \& push @$ref_to_AoA, [ split ]; |
| 305 | \& } |
| 306 | .Ve |
| 307 | .PP |
| 308 | Now you can add new rows. What about adding new columns? If you're |
| 309 | dealing with just matrices, it's often easiest to use simple assignment: |
| 310 | .PP |
| 311 | .Vb 5 |
| 312 | \& for $x (1 .. 10) { |
| 313 | \& for $y (1 .. 10) { |
| 314 | \& $AoA[$x][$y] = func($x, $y); |
| 315 | \& } |
| 316 | \& } |
| 317 | .Ve |
| 318 | .PP |
| 319 | .Vb 3 |
| 320 | \& for $x ( 3, 7, 9 ) { |
| 321 | \& $AoA[$x][20] += func2($x); |
| 322 | \& } |
| 323 | .Ve |
| 324 | .PP |
| 325 | It doesn't matter whether those elements are already |
| 326 | there or not: it'll gladly create them for you, setting |
| 327 | intervening elements to \f(CW\*(C`undef\*(C'\fR as need be. |
| 328 | .PP |
| 329 | If you wanted just to append to a row, you'd have |
| 330 | to do something a bit funnier looking: |
| 331 | .PP |
| 332 | .Vb 2 |
| 333 | \& # add new columns to an existing row |
| 334 | \& push @{ $AoA[0] }, "wilma", "betty"; |
| 335 | .Ve |
| 336 | .PP |
| 337 | Notice that I \fIcouldn't\fR say just: |
| 338 | .PP |
| 339 | .Vb 1 |
| 340 | \& push $AoA[0], "wilma", "betty"; # WRONG! |
| 341 | .Ve |
| 342 | .PP |
| 343 | In fact, that wouldn't even compile. How come? Because the argument |
| 344 | to \fIpush()\fR must be a real array, not just a reference to such. |
| 345 | .Sh "Access and Printing" |
| 346 | .IX Subsection "Access and Printing" |
| 347 | Now it's time to print your data structure out. How |
| 348 | are you going to do that? Well, if you want only one |
| 349 | of the elements, it's trivial: |
| 350 | .PP |
| 351 | .Vb 1 |
| 352 | \& print $AoA[0][0]; |
| 353 | .Ve |
| 354 | .PP |
| 355 | If you want to print the whole thing, though, you can't |
| 356 | say |
| 357 | .PP |
| 358 | .Vb 1 |
| 359 | \& print @AoA; # WRONG |
| 360 | .Ve |
| 361 | .PP |
| 362 | because you'll get just references listed, and perl will never |
| 363 | automatically dereference things for you. Instead, you have to |
| 364 | roll yourself a loop or two. This prints the whole structure, |
| 365 | using the shell-style \fIfor()\fR construct to loop across the outer |
| 366 | set of subscripts. |
| 367 | .PP |
| 368 | .Vb 3 |
| 369 | \& for $aref ( @AoA ) { |
| 370 | \& print "\et [ @$aref ],\en"; |
| 371 | \& } |
| 372 | .Ve |
| 373 | .PP |
| 374 | If you wanted to keep track of subscripts, you might do this: |
| 375 | .PP |
| 376 | .Vb 3 |
| 377 | \& for $i ( 0 .. $#AoA ) { |
| 378 | \& print "\et elt $i is [ @{$AoA[$i]} ],\en"; |
| 379 | \& } |
| 380 | .Ve |
| 381 | .PP |
| 382 | or maybe even this. Notice the inner loop. |
| 383 | .PP |
| 384 | .Vb 5 |
| 385 | \& for $i ( 0 .. $#AoA ) { |
| 386 | \& for $j ( 0 .. $#{$AoA[$i]} ) { |
| 387 | \& print "elt $i $j is $AoA[$i][$j]\en"; |
| 388 | \& } |
| 389 | \& } |
| 390 | .Ve |
| 391 | .PP |
| 392 | As you can see, it's getting a bit complicated. That's why |
| 393 | sometimes is easier to take a temporary on your way through: |
| 394 | .PP |
| 395 | .Vb 6 |
| 396 | \& for $i ( 0 .. $#AoA ) { |
| 397 | \& $aref = $AoA[$i]; |
| 398 | \& for $j ( 0 .. $#{$aref} ) { |
| 399 | \& print "elt $i $j is $AoA[$i][$j]\en"; |
| 400 | \& } |
| 401 | \& } |
| 402 | .Ve |
| 403 | .PP |
| 404 | Hmm... that's still a bit ugly. How about this: |
| 405 | .PP |
| 406 | .Vb 7 |
| 407 | \& for $i ( 0 .. $#AoA ) { |
| 408 | \& $aref = $AoA[$i]; |
| 409 | \& $n = @$aref - 1; |
| 410 | \& for $j ( 0 .. $n ) { |
| 411 | \& print "elt $i $j is $AoA[$i][$j]\en"; |
| 412 | \& } |
| 413 | \& } |
| 414 | .Ve |
| 415 | .Sh "Slices" |
| 416 | .IX Subsection "Slices" |
| 417 | If you want to get at a slice (part of a row) in a multidimensional |
| 418 | array, you're going to have to do some fancy subscripting. That's |
| 419 | because while we have a nice synonym for single elements via the |
| 420 | pointer arrow for dereferencing, no such convenience exists for slices. |
| 421 | (Remember, of course, that you can always write a loop to do a slice |
| 422 | operation.) |
| 423 | .PP |
| 424 | Here's how to do one operation using a loop. We'll assume an \f(CW@AoA\fR |
| 425 | variable as before. |
| 426 | .PP |
| 427 | .Vb 5 |
| 428 | \& @part = (); |
| 429 | \& $x = 4; |
| 430 | \& for ($y = 7; $y < 13; $y++) { |
| 431 | \& push @part, $AoA[$x][$y]; |
| 432 | \& } |
| 433 | .Ve |
| 434 | .PP |
| 435 | That same loop could be replaced with a slice operation: |
| 436 | .PP |
| 437 | .Vb 1 |
| 438 | \& @part = @{ $AoA[4] } [ 7..12 ]; |
| 439 | .Ve |
| 440 | .PP |
| 441 | but as you might well imagine, this is pretty rough on the reader. |
| 442 | .PP |
| 443 | Ah, but what if you wanted a \fItwo-dimensional slice\fR, such as having |
| 444 | \&\f(CW$x\fR run from 4..8 and \f(CW$y\fR run from 7 to 12? Hmm... here's the simple way: |
| 445 | .PP |
| 446 | .Vb 6 |
| 447 | \& @newAoA = (); |
| 448 | \& for ($startx = $x = 4; $x <= 8; $x++) { |
| 449 | \& for ($starty = $y = 7; $y <= 12; $y++) { |
| 450 | \& $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y]; |
| 451 | \& } |
| 452 | \& } |
| 453 | .Ve |
| 454 | .PP |
| 455 | We can reduce some of the looping through slices |
| 456 | .PP |
| 457 | .Vb 3 |
| 458 | \& for ($x = 4; $x <= 8; $x++) { |
| 459 | \& push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ]; |
| 460 | \& } |
| 461 | .Ve |
| 462 | .PP |
| 463 | If you were into Schwartzian Transforms, you would probably |
| 464 | have selected map for that |
| 465 | .PP |
| 466 | .Vb 1 |
| 467 | \& @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8; |
| 468 | .Ve |
| 469 | .PP |
| 470 | Although if your manager accused of seeking job security (or rapid |
| 471 | insecurity) through inscrutable code, it would be hard to argue. :\-) |
| 472 | If I were you, I'd put that in a function: |
| 473 | .PP |
| 474 | .Vb 5 |
| 475 | \& @newAoA = splice_2D( \e@AoA, 4 => 8, 7 => 12 ); |
| 476 | \& sub splice_2D { |
| 477 | \& my $lrr = shift; # ref to array of array refs! |
| 478 | \& my ($x_lo, $x_hi, |
| 479 | \& $y_lo, $y_hi) = @_; |
| 480 | .Ve |
| 481 | .PP |
| 482 | .Vb 4 |
| 483 | \& return map { |
| 484 | \& [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ] |
| 485 | \& } $x_lo .. $x_hi; |
| 486 | \& } |
| 487 | .Ve |
| 488 | .SH "SEE ALSO" |
| 489 | .IX Header "SEE ALSO" |
| 490 | \&\fIperldata\fR\|(1), \fIperlref\fR\|(1), \fIperldsc\fR\|(1) |
| 491 | .SH "AUTHOR" |
| 492 | .IX Header "AUTHOR" |
| 493 | Tom Christiansen <\fItchrist@perl.com\fR> |
| 494 | .PP |
| 495 | Last update: Thu Jun 4 16:16:23 \s-1MDT\s0 1998 |